2
Outline Introduction to Inheritance in OOP Superclasses and Subclasses Relationship between Superclasses and Subclasses Constructors in Subclasses Object Class

3
Introduction to Inheritance in OOP Inheritance is one of the primary features of OOP It is a form of software reuse. –Superclass vs. Subclass –“is-a” relationship. –A subclass can be created by absorbing an existing superclass’s members and embellishing them with new or modified capability.

4
Superclasses and Subclasses (1/3) A subclass possesses all attributes and methods of its superclass. Additionally, it has its own attributes and methods. –Note the subclass’ own methods can be new or modified versions of superclass’ methods. The “is-a” relationship between a subclass and superclass can be direct or indirect.

7
Relationship between Superclasses and Subclasses (1/18) Rules: –A subclass can only have one direct superclass. –All members of the superclass become members of the subclass. –But the superclass’ private members are NOT directly accessible in the subclass’ methods. They have to be accessed through the superclass’ public methods. –The first statement of the subclass’ constructor is invoking one of the direct superclass’ constructor.

14
Relationship between Superclasses and Subclasses (8/18) Rules: (cont) –A subclass can only have one direct superclass. –All members of the superclass become members of the subclass. –But the superclass’ private members are not directly accessible in the subclass’ methods. They have to be accessed through the superclass’ public methods. –The first statement of the subclass’ constructor is invoking one of the direct superclass’ constructor.

18
Relationship between Superclasses and Subclasses (12/18) Rules: (cont) –A superclass’s protected members can be accessed by Members of that superclass Members of its subclass (direct or indirect) Members of other classes in the same package.

19
Relationship between Superclasses and Subclasses (13/18) // in BasePlusCommissionEmployee // and commissionRate and grossSales // are private in the superclass. 1.public double earnings(){ 2. return baseSalary+ 3. getCommissionRate()*getGrossSales(); 4.} It will be great if we can call the superclass’s method earnings.

20
Relationship between Superclasses and Subclasses (14/18) Rules: –The redefined methods in the superclass can be accessed in its subclasses by preceding the method name with “super.”

23
Relationship between Superclasses and Subclasses (17/18) Rules: –A subclass can only have one direct superclass. –All members of the superclass become members of the subclass. –But the superclass’ private members are not directly accessible in the subclass’ methods. They have to be accessed through the superclass’ public methods. –The first statement of the subclass’ constructor is invoking one of the direct superclass’ constructor.

24
Relationship between Superclasses and Subclasses (18/18) Rules: (cont) –A superclass’ method can be redefined in the subclass. –A superclass’s protected members can be accessed by Members of that superclass Members of its subclass (direct or indirect) Members of other classes in the same package. –The redefined methods in the superclass can be accessed in its subclasses by preceding the method name with “super.”

25
Object Class All classes in Java inherit directly or indirectly from the Object class (package java.lang), so its 11 methods are inherited by all other classes. –clone() –equals() –finalize() –getClass() –hashCode() –wait() - three versions –notify() –notifyAll() –toString()