Inheritance In objectoriented programming a mechanism called inheritance
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 (non-static mainly, but also static). 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.
Sample Inheritance Hierarchy Account Savings Super. Saver Regular Checking Student Interest Bearing ATM Checking
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 defines non-static things that are in the superclass, the subclass elements override the superclass elements If a subclass defines static things that are in the superclass, the subclass elements hide the superclass elements
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>
Example Program Ding … out to reality … Cold. Medicine. java, Cold. Pills. java, Cold. Syrup. java, Use. Cold. Medicine. java
Inheritance and Member Accessibility Which members of a superclass are accessible from its subclasses? Superclass There is a third visibility modifier called protected. Subclass public private protected
Access from the Outside Only the public elements are accessible from the outside objects. my. Superclass my. Subclass Superclass Client test
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. my. Subclass Superclass accessible inaccessible
Example Program Dong … out to reality … Student. java, Graduate. Student. java, Undergraduate. Student. java, Use. Student 1. java Dung … out to reality … House. Occupant. java, Human. java, Pet. java, Use. House. Occupant. java
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 Graduate. Student(); student. compute. Grade( ); Student student; student = new Undergraduate. Student(); student. compute. Grade( ); This will call the method of Graduate. Student This will call the method of Undergraduate. Student
Creating the roster Array Student roster[ ] = new Student[40]; roster[0] roster[1] roster[2] roster[3] roster Graduate. Student = = new new Graduate. Student( ); Undergraduate. Student( ); Graduate. Student( ); 0 Undergraduate. Student 1 2 Undergraduate. Student 3 4 Graduate. Student roster is an array of Student objects. Create instances of the subclasses of Student. 36 37 38 39
Example Programs Dung … out to reality … Student. java, Graduate. Student. java, Undergraduate. Student. java, Use. Student 2. java
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.
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.
Sample Abstract Class public abstract class Student {. . . public abstract void compute. Grade( ); . . . } Reserved word abstract in the class declaration. Abstract method has no method body. public class Graduate. Student extends Student {. . . public void compute. Grade( ) { //method body comes here }. . . } Abstract method is must be fully implemented in the descendant class.
Example Programs Deng … out to reality … H 2 O. java, Ice. java, Water. java, Steam. java, Use. H 2 O. java
- Slides: 16