250 likes | 383 Views
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.
E N D
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 Student Alumnus Faculty Staff Administrator Teacher
Superclasses and Subclasses (3/3) Shape TwoDimensionalShape ThreeDimensionalShape Circle Square Triangle 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) • 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.
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; • }
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); • }
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?
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) • // 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
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. • public double earnings(){ • return baseSalary+ • getCommissionRate()*getGrossSales(); • } 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. • public double earnings(){ • return baseSalary+ • getCommissionRate()*getGrossSales(); • } super.earnings();
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); • }
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()