COP 3003 Object-Oriented Programming - Inheritance

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

description

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. - PowerPoint PPT Presentation

Transcript of COP 3003 Object-Oriented Programming - Inheritance

Page 1: COP 3003 Object-Oriented Programming - Inheritance

COP 3003 Object-Oriented Programming - Inheritance

Dr. Janusz Zalewski, Fall 2013

Prepared by Dr Dahai Guo

Page 2: COP 3003 Object-Oriented Programming - Inheritance

Outline

• Introduction to Inheritance in OOP

• Superclasses and Subclasses

• Relationship between Superclasses and Subclasses

• Constructors in Subclasses

• Object Class

Page 3: COP 3003 Object-Oriented Programming - Inheritance

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.

Page 4: COP 3003 Object-Oriented Programming - Inheritance

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.

Page 5: COP 3003 Object-Oriented Programming - Inheritance

Superclasses and Subclasses (2/3)

CommunityMember

Employee Student Alumnus

Faculty Staff

Administrator Teacher

Page 6: COP 3003 Object-Oriented Programming - Inheritance

Superclasses and Subclasses (3/3)

Shape

TwoDimensionalShape ThreeDimensionalShape

Circle Square Triangle Sphere Cube Tetrahedron

Page 7: COP 3003 Object-Oriented Programming - Inheritance

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.

Page 8: COP 3003 Object-Oriented Programming - Inheritance

Relationship between Superclasses and Subclasses (2/18)

• Example: – CommissionEmploye:

salary is only based on sales– BasePlusCommissionEmployee:

salary = commission + baseSalary

Page 9: COP 3003 Object-Oriented Programming - Inheritance

Relationship between Superclasses and Subclasses (3/18)

CommissionEmployee

firstName

lastName

socialSecurityNumber

grossSales

commissionRate

BasePlusCommissionEmployee

firstName

lastName

socialSecurityNumber

grossSales

commissionRate

baseSalary

extends

Page 10: COP 3003 Object-Oriented Programming - Inheritance

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.

Page 11: COP 3003 Object-Oriented Programming - Inheritance

Relationship between Superclasses and Subclasses (5/18)

1. public class BasePlusCommissionEmployee 2. extends CommissionEmployee3. {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. }

Page 12: COP 3003 Object-Oriented Programming - Inheritance

Relationship between Superclasses and Subclasses (6/18)

1. // in CommissionEmployee2. 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. }

Page 13: COP 3003 Object-Oriented Programming - Inheritance

Relationship between Superclasses and Subclasses (7/18)

1. // in BasePlusCommissionEmployee2. // BasePlusCommissionEmployee inherits CommissionEmployee3. 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?

Page 14: COP 3003 Object-Oriented Programming - Inheritance

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.

Page 15: COP 3003 Object-Oriented Programming - Inheritance

Relationship between Superclasses and Subclasses (9/18)

1. // in BasePlusCommissionEmployee2. 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

Page 16: COP 3003 Object-Oriented Programming - Inheritance

Relationship between Superclasses and Subclasses (10/18)

• Rules: (cont)– A superclass’ method can be redefined in the

subclass.

Page 17: COP 3003 Object-Oriented Programming - Inheritance

Relationship between Superclasses and Subclasses (11/18)

• Sometimes, it is desirable to directly access superclass’ instance variables.

• Access modifier protected makes it possible.

Page 18: COP 3003 Object-Oriented Programming - Inheritance

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.

Page 19: COP 3003 Object-Oriented Programming - Inheritance

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.

Page 20: COP 3003 Object-Oriented Programming - Inheritance

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

Page 21: COP 3003 Object-Oriented Programming - Inheritance

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

Page 22: COP 3003 Object-Oriented Programming - Inheritance

Relationship between Superclasses and Subclasses (16/18)

1. // in CommissionEmployee2. 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 BasePlusCommissionEmployee10. pulbic String toString(){11. return String.format(“%s %s\n%s: %.2f”,12. “base-salaried”, super.toString(), 13. “base salary”, baseSalary);14. }

Page 23: COP 3003 Object-Oriented Programming - Inheritance

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.

Page 24: COP 3003 Object-Oriented Programming - Inheritance

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

Page 25: COP 3003 Object-Oriented Programming - Inheritance

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