1 / 25

COP 3003 Object-Oriented Programming - Inheritance

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.

Download Presentation

COP 3003 Object-Oriented Programming - Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 Employee Student Alumnus Faculty Staff Administrator Teacher

  6. Superclasses and Subclasses (3/3) Shape TwoDimensionalShape ThreeDimensionalShape Circle Square Triangle Sphere Cube Tetrahedron

  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) extends CommissionEmployee BasePlusCommissionEmployee firstName baseSalary lastName firstName socialSecurityNumber lastName grossSales socialSecurityNumber commissionRate grossSales commissionRate

  10. Relationship between Superclasses and Subclasses (4/18) • public class CommissionEmployee • extends Object { • private String firstName; • private String lastName; • private String socialSecurityNumber; • private double grossSales; • private double commissionRate; • } Every class in Java extends Object implicitly or explicitly.

  11. Relationship between Superclasses and Subclasses (5/18) • public class BasePlusCommissionEmployee • extends CommissionEmployee • { • private double baseSalary; • private String firstName; • private String lastName; • private String socialSecurityNumber; • private double grossSales; • private double commissionRate; • }

  12. Relationship between Superclasses and Subclasses (6/18) • // in CommissionEmployee • public CommissionEmployee(String first, • String last, String ssn, double sales, • double rate) { • firstName=first; • lastName=last; • socialSecurityNumber=ssn; • setGrossSales(sales); • setCommissionRate(rate); • }

  13. Relationship between Superclasses and Subclasses (7/18) • // in BasePlusCommissionEmployee • // BasePlusCommissionEmployee inherits CommissionEmployee • public BasePlusCommissionEmployee(String first, • String last, String ssn, double sales, • double rate, double base) { • firstName=first; • lastName=last; • socialSecurityNumber=ssn; • setGrossSales(sales); • setCommissionRate(rate); • setBaseSalary(base); • } 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) • // in BasePlusCommissionEmployee • public BasePlusCommissionEmployee(String first, • String last, String ssn, double sales, • double rate, double base){ • firstName=first; • lastName=last; • socialSecurityNumber=ssn; • setGrossSales(sales); • setCommissionRate(rate); • setBaseSalary(base); • } 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. • public double earnings(){ • return baseSalary+ • getCommissionRate()*getGrossSales(); • } 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. • public double earnings(){ • return baseSalary+ • getCommissionRate()*getGrossSales(); • } super.earnings();

  22. Relationship between Superclasses and Subclasses (16/18) • // 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); • }

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

More Related