Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 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

7 Deriving Subclasses Words.java (extra) Book.java (extra) Dictionary.java (extra) BankAccount.java SavingAccount.java UseSavingAccount.java CheckingAccount.java UseCheckingAccount.java

8 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

9 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();

10 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)

11 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

12 Polymorphism Compile-time vs. run-time types Animal 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); where public void feed(Animal[] theZoo) { for (int i = 0; i < theZoo.length; i++) theZoo[i].eat(); }

13 Polymorphism Animal.java Lion.java Giraffe.java UseAnimals.java Duck.java UseNewAnimals.java Withdraw.java

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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,...


Download ppt "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."

Similar presentations


Ads by Google