9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6...

24
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios

Transcript of 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6...

Page 1: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

9. Inheritance9.1 Subclasses

9.2 Polymorphism

9.3 Abstract Classes

9.4 Modifiers and Access

9.6 Object-Oriented Design with Use Cases and Scenarios

Page 2: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

Objectives

• Use inheritance to relate classes• Use polymorphism to improve design• Use abstract classes to achieve implementation

independence• Understand the use of modifiers in specifying

access• Develop an object-oriented design from use cases

and scenarios

Page 3: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

Subclasses

• Inheritance allows deriving a new class from an existing one

• The existing class is called the parent class, or superclass, or base class

• The derived class is called the child class or subclass

• The child class inherits characteristics: data and methods, of the parent

• Java only support single inheritance

Page 4: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

Subclasses

• The inheritance (child-parent) relationships are often shown graphically in a class diagram like

• Inheritance exhibits an is-a relationship, meaning the child is a more specific version of the parent

Vehicle

Car

Page 5: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

Class Hierarchies

• Inheritance relationships create hierarchy of classes

• Two children of the same parent are called siblings

• An inherited member is continually passed down the line

• Class hierarchies often have to be extended and modified to keep up with changing needs

Page 6: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

Deriving Subclasses

• To create a subclass, we extend a class, inheriting all the attributes and behavior of that class

public class Car extends Vehicle

{ // class contents

}

• The keyword extends tells us that Car is a subclass of the superclass Vehicle

• A subclass usually have data and methods defined in addition to the ones inherited

Page 7: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

Deriving Subclasses

• Words.java (extra)

Book.java (extra)

Dictionary.java (extra)• BankAccount.java

SavingAccount.java

UseSavingAccount.java

CheckingAccount.java

UseCheckingAccount.java

Page 8: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

Deriving Subclasses

• A child class inherits the public methods of its parent, but it may choose to override some of them to implement its own specific behavior

Messages.java (extra)

Thought.java (extra)

Advice.java (extra)

• Overloading - multiple methods in the same class with the same name but different signatures

• Overriding - two methods of the same name, one in a parent class and one in a child class, that have the same signature

Page 9: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

Reference and Inheritance

• An object reference can refer to an object of its class, or to an object of any class related to it by inheritance

• For example, if the Christmas is a subclass of the Holiday class, then a Holiday reference can be used to point to a Christmas object

Holiday day;

day = new Christmas();

Page 10: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

The Keyword super• Constructors are not inherited, even though they

are publicly visible• Java uses the keyword super to reference the

superclass, it is often used to invoked the parent’s constructor

• Words2.java (extra)

Book2.java (extra)

Dictionary2.java (extra)

Page 11: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

Polymorphism• Polymorphism - “many forms”• A polymorphic reference is one which can refer to

different types of objects at different times• A reference to the superclass can actually refer to

an object in any subclass• An interface can also be used to create polymorphic

reference• An object reference variable has

– Compile-time type - the variable’s declared type

– Run-time type - the type of the object to which the variable currently refers

Page 12: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

Polymorphism

• Compile-time vs. run-time typesAnimal herman; // compile-time type Animal

Animal zelda; // compile-time type Animal

herman = new Lion(); // run-time type Lion

zelda = new Giraffe(); // run-time type Giraffe

Animal[] two = {herman, zelda};

feed(two);

wherepublic void feed(Animal[] theZoo) {

for (int i = 0; i < theZoo.length; i++)

theZoo[i].eat();

}

Page 13: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

Polymorphism

• Animal.java

Lion.java

Giraffe.java

UseAnimals.java• Duck.java

UseNewAnimals.java• Withdraw.java

Page 14: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

Abstract Classes

• An abstract class is a placeholder in a class hierarchy that represents a generic concept

• An abstract class cannot be instantiate• We use the modifier abstract on the class header to

declare a class as abstract• An abstract class often contains abstract methods

(like an interface does), though it doesn’t have to• The child of an abstract class must override the

abstract methods of the parent, or it too will be considered abstract

Page 15: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

Abstract Classes

• The keyword final - Specify that a method cannot be overridden

• An Abstract method cannot be defined as final or static

• The use of abstract classes is a design decision; it helps us establish common elements in a class that is too general to instantiate

• Polymorphism often works with abstract classes• Operations of an abstract class will work for

objects of any subclass

Page 16: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

A Shape Classes

• Shape is an abstract concept• Shape has subclass like Line, Circle, etc. that have

instances• Shape.java• Line.java• Circle.java• UseShape.java

Page 17: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

Package

• A java package corresponds to a directory in the directory structure of the host machine

• To put a class in a named package, use a package statement as the first statement in the file

• For example: package personData;• The compiled byte code file (.class) must be

placed under the directory with the same name of the package

• An import statement tells Java to look in the directory for classes referenced in the program

Page 18: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

Package

• Using import statement, we can ommit the package prefix of the classes

• In addition to the default directories that Java will automatically search, the

• classpath enviroment variable specifies user-defined directories to search

• For example:

javac -classpath .;c:\booklr Test.java

java -classpath .;c:\booklr Test

Page 19: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

Modifiers and Access• public - anywhere the class name is accessible• protected - in the package that contains the

class in which the member is declared, and in any subclass of that class

• (no modifier) - in the package that contains the class in which the member is declared

• private - only in the class in which the member is declared

• The visibility package: A.java, B.java, C.java, D.java

Page 20: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

OO Design

• Understand the problem• Solve the problem by identifying objects• Each object provides certain services• Objects use each other's services• A use case defines one function that the system

should provide• For each case, several scenarios to describe the

interaction among the user and• other parts of the system to provide the function

Page 21: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

OO Design Example

• Problem - Simple automatic teller machine• Use cases

– deposit

– withdrawal

– get balance

• Several scenarios for each use case– at least one for success– several for failure

• AtmScreen.java

Page 22: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

A Success Scenario for Deposit• The user asks the teller to accept an ATM card

• The teller asks the user to enter a PIN

• The user asks the teller to accept a PIN

• The teller asks the user to select a transaction type

• The user asks the teller to accept a deposit

• The teller asks the user to select an account type

• The user asks the teller to accept a saving account type

• The teller asks the bank to find the account of the chosen type for the user with the specified PIN

• The bank gives the teller a reference to the account

Page 23: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

A Success Scenario for Deposit• The teller asks the user to specify an amount

• The user asks the teller to accept an amount

• The teller asks the account to de[posit the specified amount

• The teller asks the user to select another transaction

Page 24: 9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.

A Failure Scenario• The user asks the teller to accept an ATM card

• The teller asks the user to enter a PIN

• The user asks the teller to accept a PIN

• The teller asks the user to select a transaction type

• The user asks the teller to accept a get balance transaction type

• The teller asks the user to select an account type

• The user asks the teller to accept a checking account type

• The teller asks the bank to find the account of the chosen type for the user with the specified PIN

• The bank gives the teller a null account

• The teller asks the user to select another transaction, ...