COP 3003 Object-Oriented Programming - Inheritance Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo Outline • Introduction to Inheritance in OOP • Superclasses and Subclasses • Relationship between Superclasses and Subclasses • Constructors in Subclasses • Object Class 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. 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. Superclasses and Subclasses (2/3) CommunityMember Employee Faculty Administrator Student Staff Teacher Alumnus Superclasses and Subclasses (3/3) Shape TwoDimensionalShape Circle Square Triangle ThreeDimensionalShape Sphere Cube Tetrahedron 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. Relationship between Superclasses and Subclasses (2/18) • Example: – CommissionEmploye: salary is only based on sales – BasePlusCommissionEmployee: salary = commission + baseSalary Relationship between Superclasses and Subclasses (3/18) extends CommissionEmployee BasePlusCommissionEmployee firstName baseSalary lastName firstName socialSecurityNumber lastName grossSales socialSecurityNumber commissionRate grossSales commissionRate Relationship between Superclasses and Subclasses (4/18) 1. public class CommissionEmployee 2. extends Object { 3. private String firstName; 4. private String lastName; 5. private String socialSecurityNumber; 6. private double grossSales; 7. private double commissionRate; 8. } Every class in Java extends Object implicitly or explicitly. Relationship between Superclasses and Subclasses (5/18) 1. public class BasePlusCommissionEmployee 2. extends CommissionEmployee 3. { 4. private double baseSalary; 5. private String firstName; 6. private String lastName; 7. private String socialSecurityNumber; 8. private double grossSales; 9. private double commissionRate; 10. } Relationship between Superclasses and Subclasses (6/18) 1. // in CommissionEmployee 2. public CommissionEmployee(String first, 3. String last, String ssn, double sales, 4. double rate) { 5. firstName=first; 6. lastName=last; 7. socialSecurityNumber=ssn; 8. setGrossSales(sales); 9. setCommissionRate(rate); 10. } Relationship between Superclasses and Subclasses (7/18) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. // in BasePlusCommissionEmployee // BasePlusCommissionEmployee inherits CommissionEmployee public BasePlusCommissionEmployee(String first, String last, String ssn, double sales, double rate, double base) { firstName=first; lastName=last; Anything wrong? socialSecurityNumber=ssn; setGrossSales(sales); setCommissionRate(rate); setBaseSalary(base); } 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. Relationship between Superclasses and Subclasses (9/18) 1. // in BasePlusCommissionEmployee 2. public BasePlusCommissionEmployee(String first, 3. String last, String ssn, double sales, 4. double rate, double base){ 5. firstName=first; 6. lastName=last; 7. socialSecurityNumber=ssn; 8. setGrossSales(sales); 9. setCommissionRate(rate); super(first, last, ssn, sales, rate); 10. setBaseSalary(base); // calls the superclass’ 11. } // constructor // must be the first statement Relationship between Superclasses and Subclasses (10/18) • Rules: (cont) – A superclass’ method can be redefined in the subclass. Relationship between Superclasses and Subclasses (11/18) • Sometimes, it is desirable to directly access superclass’ instance variables. • Access modifier protected makes it possible. 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. 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. 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.” Relationship between Superclasses and Subclasses (15/18) • // in BasePlusCommissionEmployee • // and commissionRate and grossSales • // are private in the superclass. 1.public double earnings(){ 2. return baseSalary+ 3. getCommissionRate()*getGrossSales(); super.earnings(); 4.} Relationship between Superclasses and Subclasses (16/18) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. // in CommissionEmployee public String toString(){ return String.format(“%s: %s %s\n%s: %s\n%s: %.2f\n%s %.2f”, “commission employee”, firstName, lastName, “ssn”, socialSecurity, “gross sales”, grossSales, “commission rate”, commissionRate); } // in BasePlusCommissionEmployee pulbic String toString(){ return String.format(“%s %s\n%s: %.2f”, “base-salaried”, super.toString(), “base salary”, baseSalary); } 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. 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.” 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()