Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 3003 Object-Oriented Programming - Inheritance Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo.

Similar presentations


Presentation on theme: "COP 3003 Object-Oriented Programming - Inheritance Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo."— Presentation transcript:

1 COP 3003 Object-Oriented Programming - Inheritance Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo

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.

5 Superclasses and Subclasses (2/3) CommunityMember EmployeeStudentAlumnus FacultyStaff AdministratorTeacher

6 Superclasses and Subclasses (3/3) Shape TwoDimensionalShapeThreeDimensionalShape CircleSquareTriangleSphereCubeTetrahedron

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.

8 Relationship between Superclasses and Subclasses (2/18) Example: –CommissionEmploye: salary is only based on sales –BasePlusCommissionEmployee: salary = commission + baseSalary

9 Relationship between Superclasses and Subclasses (3/18) CommissionEmployee firstName lastName socialSecurityNumber grossSales commissionRate BasePlusCommissionEmployee firstName lastName socialSecurityNumber grossSales commissionRate baseSalary extends

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

11 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.}

12 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.}

13 Relationship between Superclasses and Subclasses (7/18) 1.// in BasePlusCommissionEmployee 2.// BasePlusCommissionEmployee inherits CommissionEmployee 3.public BasePlusCommissionEmployee(String first, 4. String last, String ssn, double sales, 5. double rate, double base) { 6. firstName=first; 7. lastName=last; 8. socialSecurityNumber=ssn; 9. setGrossSales(sales); 10. setCommissionRate(rate); 11. setBaseSalary(base); 12.} Anything wrong?

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.

15 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); 10. setBaseSalary(base); 11.} super(first, last, ssn, sales, rate); // calls the superclass’ // constructor // must be the first statement

16 Relationship between Superclasses and Subclasses (10/18) Rules: (cont) –A superclass’ method can be redefined in the subclass.

17 Relationship between Superclasses and Subclasses (11/18) Sometimes, it is desirable to directly access superclass’ instance variables. Access modifier protected makes it possible.

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.”

21 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(); 4.} super.earnings();

22 Relationship between Superclasses and Subclasses (16/18) 1.// in CommissionEmployee 2.public String toString(){ 3. return String.format(“%s: %s %s\n%s: %s\n%s: %.2f\n%s %.2f”, 4. “commission employee”, firstName, lastName, 5. “ssn”, socialSecurity, 6. “gross sales”, grossSales, 7. “commission rate”, commissionRate); 8.} 9.// in BasePlusCommissionEmployee 10.pulbic String toString(){ 11. return String.format(“%s %s\n%s: %.2f”, 12. “base-salaried”, super.toString(), 13. “base salary”, baseSalary); 14.}

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


Download ppt "COP 3003 Object-Oriented Programming - Inheritance Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo."

Similar presentations


Ads by Google