COMP201 Java Programming Topic 4: Inheritance Readings: Chapter 5

  • View
    215

  • Download
    0

Embed Size (px)

Transcript

  • Slide 1
  • COMP201 Java Programming Topic 4: Inheritance Readings: Chapter 5
  • Slide 2
  • COMP201 Topic 4 / Slide 2 Objectives and Outline l Objectives: n Understand java inheritance and learn to use it. l Outline n Introduction: concept of inheritance n Deriving a subclass n Using subclasses n Special class types and classes arising from inheritance Abstract classes Final classes The Object class (The Class class allows you to analyze and manipulate java program at run time.)
  • Slide 3
  • COMP201 Topic 4 / Slide 3 Introduction to Inheritance l Technique for deriving a new class from an existing class. l Existing class called superclass, base class, or parent class. l New class is called subclass, derived class, or child class.
  • Slide 4
  • COMP201 Topic 4 / Slide 4 l Subclass and superclass are closely related n Subclass share fields and methods of superclass n Subclass can have more fields and methods n Implementations of a method in superclass and subclass can be different n An object of subclass is automatically an object of superclass, but not vice versa The set of subclass objects is a subset of the set of superclass objects. (E.g. The set of Manager s is a subset of the set of Employee s.) This explains the term subclass and superclass. Introduction to Inheritance
  • Slide 5
  • COMP201 Topic 4 / Slide 5 Introduction to Inheritance Why inheritance? Employee class: name, salary, hireDay; getName, raiseSalary(), getHireDay(). Manager is-a Employee, has all above, and l Has a bonus l getsalary() computed differently Instead of defining Manager class from scratch, one can derive it from the Employee class. Work saved.
  • Slide 6
  • COMP201 Topic 4 / Slide 6 Introduction to Inheritance Checking method A method B1 Saving method A method B2 Account method A checking method B1 saving method B2 Why inheritance? Inheritance allows one to factor out common functionality by moving it to a superclass, results in better program.
  • Slide 7
  • COMP201 Topic 4 / Slide 7 Introduction to Inheritance l Multiple inheritance n A class extends >1 superclasses l Java does not support multiple inheritance n A java class can only extend ONE superclass n Functionality of multiple inheritance recovered by interfaces.
  • Slide 8
  • COMP201 Topic 4 / Slide 8 Outline l Outline n Introduction: concept of inheritance n Deriving a subclass n Using subclasses n Special class types and classes arising from inheritance Abstract classes Final classes The Object class
  • Slide 9
  • COMP201 Topic 4 / Slide 9 Deriving a Subclass l General scheme for deriving a subclass: class subClassName extends superClassName { constructors refined methods additional methods additional fields } Indicate the differences between subclass and superclass
  • Slide 10
  • COMP201 Topic 4 / Slide 10 Deriving a class class Employee { public Employee(String n, double s, int year, int month, int day) {} public String getName(){} public double getSalary() {} public Data getHireDay(){} public void raiseSalary(double byPercent) {} private String name; private double Salary; private Date hireDay; }
  • Slide 11
  • COMP201 Topic 4 / Slide 11 Deriving a class Extending Employee class to get Manager class class Manager extends Employee { public Manager() {} // constructor public void getSalary() {} // refined method // additional methods public void setBonus(double b){} // additional field private double bonus; }
  • Slide 12
  • COMP201 Topic 4 / Slide 12 Deriving a Class l Plan n Fields of subclass n Constructors of subclass n Methods of subclass n A few notes
  • Slide 13
  • COMP201 Topic 4 / Slide 13 Fields of subclass l Semantically: Fields of superclass + additional fields n Employee Name, salary, hireday n Manager name, salary, hireday bonus l Methods in subclass cannot access private fields of superclass. n After all, subclass is another class viewed from super class. n More on this later.
  • Slide 14
  • COMP201 Topic 4 / Slide 14 Fields of subclass l Static instance fields are inherited but not duplicated in subclass. class Employee // StaticInherit.java { public Employee ( ) { numCreated++; } public static int getNumCreated() { return numCreated; } private static int numCreated=0; } Manager b = new Manager( ); // numCreated = 1 Employee e = new Employee( ); // numCreated = 2 Employee.getNumCreated(); // 2 Manager.getNumCreated(); // 2
  • Slide 15
  • COMP201 Topic 4 / Slide 15 Fields of subclass To count number of Managers separately, declare a new static variable in Manager class class Manager extends Employee { public Manager ( ) { numManager++; } public static int getNumCreated() { return numManager; } private static int numManager=0; } // StaticInherit1.java
  • Slide 16
  • COMP201 Topic 4 / Slide 16 Deriving a Class l Plan n Fields of subclass n Constructors of subclass n Methods of subclass n A few notes
  • Slide 17
  • COMP201 Topic 4 / Slide 17 Constructors of Subclass l Every constructor of a subclass must, directly or indirectly, invoke a constructor of its superclass to initialize fields of the superclass. (Subclass cannot access them directly) Use keyword super to invoke constructor of the superclass. public Manager(String n, double s, int year, int month, int day) { super(n, s, year, month, day); bonus = 0; } Must be the first line
  • Slide 18
  • COMP201 Topic 4 / Slide 18 l Can call another constructor of subclass. n Make sure that constructor of superclass is eventually called. public Manager(String n) { this(n, 0.0, 0, 0, 0); } Constructors of Subclass
  • Slide 19
  • COMP201 Topic 4 / Slide 19 l If subclass constructor does not call a superclass constructor explicitly, then superclass uses its default constructor. class FirstFrame extends JFrame { public FirstFrame() { setTitle("FirstFrame"); setSize(300, 200); } } If superclass has no default constructor, compiler error Constructor of Subclass super() implicitly called here
  • Slide 20
  • COMP201 Topic 4 / Slide 20 l Constructors are not inherited. Lets say Employee has two constructors public Employee(String n, double s, int year, int month, int day) public Employee(String n, double s) Manager has one constructor public Manager(String n, double s, int year, int month, int day) new Manager( George , 20000, 2001, 7, 20 ); //ok new Manager( Jin , 25); //not ok Constructors of Subclass
  • Slide 21
  • COMP201 Topic 4 / Slide 21 Deriving a Class l Plan n Fields of subclass n Constructors of subclass n Methods of subclass n A few notes
  • Slide 22
  • COMP201 Topic 4 / Slide 22 Methods of Subclass l Methods of subclass include n Non-private methods of superclass that are not refined (inherited). n + Refined (overriding) methods n + Additional methods l Refined and additional methods appear in subclass
  • Slide 23
  • COMP201 Topic 4 / Slide 23 Overriding Methods Salary computation for managers are different from employees. So, we need to modify the getSalary, or provide a new method that overrides getSalary public double getSalary( ) { double baseSalary = super.getSalary(); return basesalary + bonus; } l Cannot replace the last line with salary += bonus; Because salary is private to Employee. l Cannot drop super., or else we get an infinite loop Call method of superclass
  • Slide 24
  • COMP201 Topic 4 / Slide 24 Overriding Methods l An overriding method must have the same signature (name and parameter list) as the original method. Otherwise, it is simply a new method: Original Method in Employee : public double getSalary( ){} public void raiseSalary(double byPercent){} New rather than overriding methods in Manager: public void raiseSalary(int byPercent){} public void raiseWage(double byPercent){}
  • Slide 25
  • COMP201 Topic 4 / Slide 25 Overriding Methods n An overriding method must have the same return type as the original method: The following method definition in Manager would lead to compiler error: public int getSalary( ){} An overriding method must be at least as visible as the superclass method. private methods cannot be overridden, but others (public, protected, default-access methods) can.
  • Slide 26
  • COMP201 Topic 4 / Slide 26 Additional Methods public void setBonus(double b) { bonus = b; }
  • Slide 27
  • COMP201 Topic 4 / Slide 27 Methods for Subclass Manager Inherited from Employee getName, getHirDay, raiseSalary Refined from Employee getSalary. l Additional Manager, setBonus.
  • Slide 28
  • COMP201 Topic 4 / Slide 28 Deriving a Class l Plan n Fields of subclass n Constructors of subclass n Methods of subclass n A few notes
  • Slide 29
  • COMP201 Topic 4 / Slide 29 Note about this n Refer to another constructor in the same class class Employee { public Employee(String n, double s, int year, int month, int day){} public Employee(String n) // another constructor { this(n, 0, 0,0,0); // salary default at 0 }
  • Slide 30
  • COMP201 Topic 4 / Slide 30 Note about this n Refers to the current object public Employee(String n, double s, int year, int month, int day) { this.name = n; this.salary = s; GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day); // GregorianCalendar uses 0 for January this.hireDay = calendar.ge