Download presentation
Presentation is loading. Please wait.
Published byHugh Maxwell Modified over 6 years ago
1
Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common features. First we define a class that contains the common features of the entities. Then we define classes as an extension of the common class The common class is called the superclass and all classes that inherit from it subclasses. The superclass is an ancestor of the descendant subclass. We did not distinguish the savings and checking accounts in designing the Account class. If these two types of accounts are treated in the same way, then only one class to model them is enough. However, if we have to keep track of different information or process the data differently for the savings and checking accounts, using one class to model both is not appropriate. Defining two distinct classes, for example, Savings Account and Checking Account, is not appropriate because we would end up duplicating methods. For example, if the way we deduct the amount of a withdrawal from a savings account and the check amount from a checking account is the same, then we will have to define identical methods for both classes, duplicating the same code. We avoid the problems by using inheritance.
2
Sample Inheritance Hierarchy
Account Savings Checking Interest Bearing ATM Checking SuperSaver Regular Student
3
Inheritance Subclasses inherit from their superclass
Everything that's in the superclass is inherited by the subclass. Objects created from the subclass consist of the inherited components and any new components defined in the subclass If a subclass defined something that is in the superclass, the subclass elements override the superclass elements Constructors (and other things – see later) of the superclass can be called using super() We did not distinguish the savings and checking accounts in designing the Account class. If these two types of accounts are treated in the same way, then only one class to model them is enough. However, if we have to keep track of different information or process the data differently for the savings and checking accounts, using one class to model both is not appropriate. Defining two distinct classes, for example, Savings Account and Checking Account, is not appropriate because we would end up duplicating methods. For example, if the way we deduct the amount of a withdrawal from a savings account and the check amount from a checking account is the same, then we will have to define identical methods for both classes, duplicating the same code. We avoid the problems by using inheritance.
4
Example Program Ding … out to reality … ColdMedicine.java, ColdPills.java, ColdSyrup.java, UseColdMedicine.java
5
Inheritance and Member Accessibility
Superclass Subclass Which members of a superclass are accessible from its subclasses? There is a third visibility modifier called protected. public private protected
6
Access from the Outside
Only the public elements are accessible from the outside objects. mySub Subclass Superclass mySuper Client test
7
Access from the Subclass Methods
Subclass methods can access public and protected superclass methods and data. Subclass methods cannot access private superclass methods and data. mySub Subclass Superclass accessible inaccessible
8
Accessing Superclass Data and Methods
Constructors of the superclass can be called using super() Public and protected components of the superclass that have been overridden in a subclass can be accessed using super.<the component> Note: Make sure that the superclass has a constructor that takes no arguments. If the superclass does not have one, then the call super( ) from the subclass becomes invalid. To call a constructor that takes one or more arguments, you must explicitly call it from a subclass constructor, such as public Truck( int weightLimit, String vin ) { super( vin ); cargoWeightLimit = weightLimit; }
9
Example Program Dong … out to reality … Student.java, GraduateStudent.java, UndergraduateStudent.java, UseStudent1.java Dung … out to reality … HouseOccupant.java, Human.java, Pet.java, UseHouseOccupant.java
10
Polymorphism Polymorphism allows a variable to refer to objects from different (but related by inheritance) classes. References to data or methods are correctly resolved according to the object’s class. Requires that the superclass have the inherited data or method Student student; student = new GraduateStudent(); student.computeGrade( ); This will call the method of GraduateStudent Student student; student = new UndergraduateStudent(); student.computeGrade( ); This will call the method of UndergraduateStudent
11
Creating the roster Array
roster is an array of Student objects. Student roster[ ] = new Student[40]; roster[0] = new GraduateStudent( ); roster[1] = new UndergraduateStudent( ); roster[2] = new UndergraduateStudent( ); roster[3] = new GraduateStudent( ); Create instances of the subclasses of Student. 1 2 3 4 36 37 38 39 roster Graduate- Student Under- graduate- Student
12
Example Programs Dung … out to reality … Student.java, GraduateStudent.java, UndergraduateStudent.java, UseStudent2.java
13
Abstract Classes Often we want to place elements common to all subclasses in their superclass. But we do not want any instances to be created from the superclass. In such case, we designate the superclass as an abstract class. This is also a way to enforce existence of methods in subclasses.
14
Abstract Methods It is common for an abstract class to include an abstract method that must be implemented by the descendant classes. If a class includes an abstract method, then it is considered as an abstract class and must have the abstract modifier in the class declaration.
15
Sample Abstract Class abstract class Student { . . .
abstract public void computeGrade( ); } Reserved word abstract in the class declaration. Abstract method has no method body. class GraduateStudent extends Student { . . . public void computeGrade( ) //method body comes here } Abstract method is must be fully implemented in the descendant class.
16
Example Programs Deng … out to reality … H2O.java, Ice.java, Water.java, Steam.java, UseH2O.java
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.