34
1 Inheritance Chapter 9

1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

Embed Size (px)

Citation preview

Page 1: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

1

Inheritance

Chapter 9

Page 2: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

2

Module Outcomes

• To develop a subclass from a superclass through inheritance

• To invoke the superclass’s constructors and methods using the super keyword

• To override methods in the subclass• To explore the useful methods (equals(Object),

hashCode(), toString(), finalize(), clone(), and getClass()) in the Object class

• To comprehend polymorphism, dynamic binding, and generic programming

Page 3: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

3

Module Outcomes

• To describe casting and explain why explicit downcasting is necessary

• To understand the effect of hiding data fields and static methods

• To restrict access to data and methods using the protected visibility modifier

• To declare constants, unmodifiable methods, and nonextendable class using the final modifier

Page 4: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

4

Superclasses and Subclasses

Circle-radius

+getRadius+setRadius+findArea

Cylinder

-length

+getLength+setLength+findVolume

SubclassSuperclass

UML Diagram

Page 5: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

5

// Cylinder.java: Class definition for describing Cylinderpublic class Cylinder extends Circle { private double length = 1;  /** Return length */ public double getLength() { return length; }  /** Set length */ public void setLength(double length) { this.length = length; }  /** Return the volume of this cylinder */ public double findVolume() { return findArea() * length; }}

Circle-radius

+getRadius+setRadius+findArea

Cylinder

-length

+getLength+setLength+findVolume

SubclassSuperclasssupertype subtype

Page 6: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

6

Cylinder cylinder = new Cylinder();System.out.println("The length is " + cylinder.getLength());System.out.println("The radius is " + cylinder.getRadius());System.out.println("The volume of the cylinder is " + cylinder.findVolume());System.out.println("The area of the circle is " + cylinder.findArea());

The length is 1.0

The radius is 1.0

The volume of the cylinder is 3.14159

The area of the circle is 3.14159

The output is

Page 7: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

7

Using the Keyword super

• To call a superclass constructor

• To call a superclass method

The keyword super refers to the superclass of the class in which super appears. This keyword can be used in two ways:

Page 8: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

8

CAUTION: to call superclass constructor from subclass

•Must use super

•Cannot call a superclass constructor’s name in a subclass – syntax error

•statement that uses the keyword super must appear first in the constructor.

Page 9: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

9

NOTE•Properties and methods of superclass are inherited in subclass

• Superclass's constructors are NOT inherited in the subclass

•Superclass's constructors can only be invoked from the subclasses' constructors, using super.

•If the keyword super is not explicitly used, the superclass's no-arg constructor is automatically invoked.

Page 10: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

10

Superclass’s Constructor Is Always Invoked

A constructor may invoke an overloaded constructor or its superclass’s constructor. If none of them is invoked explicitly, the compiler puts super() as the first statement in the constructor. For example,

public Cylinder() {

}

is equivalent to

public Cylinder() { super();

}

public A(double d) { // some statements

}

is equivalent to

public A(double d) { super(); // some statements

}

Constructor Chaining

Page 11: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

11

Run project CircleCylinder

Study the code and design various tests

Page 12: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

12

How is inheritance made possible?

Constructor chaining

Page 13: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

13

Constructing an instance of a class invokes all the superclasses’ constructors along the inheritance chain. This is called constructor chaining.

Example:

Constructor Chaining

Page 14: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

14

Constructor Chaining

public class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { // no overloaded constructor nor super(), so call super(); System.out.println("Faculty's no-arg constructor is invoked"); }} class Employee extends Person { public Employee() { // calls overloaded constructor, no super() added this("Invoke Employee’s overloaded constructor"); System.out.println("Employee's no-arg constructor is invoked"); }  public Employee(String s) { // statement only, call super(); System.out.println(s); }} class Person { public Person() { // statement only. Insert super(); which does nothing System.out.println("Person's no-arg constructor is invoked"); }}

Page 15: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

15

Trace Executionpublic class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); }} class Employee extends Person { public Employee() { this("(2) Invoke Employee’s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); }  public Employee(String s) { System.out.println(s); }} class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); }}

1. Start from the main method

animation

Page 16: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

16

Trace Executionpublic class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); }} class Employee extends Person { public Employee() { this("(2) Invoke Employee’s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); }  public Employee(String s) { System.out.println(s); }} class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); }}

2. Invoke Faculty constructor

animation

Page 17: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

17

Trace Executionpublic class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); }} class Employee extends Person { public Employee() { this("(2) Invoke Employee’s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); }  public Employee(String s) { System.out.println(s); }} class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); }}

3. Invoke Employee’s no-arg constructor

animation

Page 18: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

18

Trace Executionpublic class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); }} class Employee extends Person { public Employee() { this("(2) Invoke Employee’s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); }  public Employee(String s) { System.out.println(s); }} class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); }}

4. Invoke Employee(String) constructor

animation

Page 19: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

19

Trace Executionpublic class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); }} class Employee extends Person { public Employee() { this("(2) Invoke Employee’s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); }  public Employee(String s) { System.out.println(s); }} class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); }}

5. Invoke Person() constructor

animation

Page 20: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

20

Trace Executionpublic class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); }} class Employee extends Person { public Employee() { this("(2) Invoke Employee’s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); }  public Employee(String s) { System.out.println(s); }} class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); }}

6. Execute println

animation

Page 21: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

21

Trace Executionpublic class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); }} class Employee extends Person { public Employee() { this("(2) Invoke Employee’s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); }  public Employee(String s) { System.out.println(s); }} class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); }}

7. Execute println

animation

Page 22: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

22

Trace Executionpublic class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); }} class Employee extends Person { public Employee() { this("(2) Invoke Employee’s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); }  public Employee(String s) { System.out.println(s); }} class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); }}

8. Execute println

animation

Page 23: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

23

Trace Executionpublic class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); }} class Employee extends Person { public Employee() { this("(2) Invoke Employee’s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); }  public Employee(String s) { System.out.println(s); }} class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); }}

9. Execute println

animation

Page 24: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

24

Order of invoking constructors

• See BJ_Faculty_0

Page 25: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

25

Example on the Impact of a Superclass without no-arg Constructor

public class Apple extends Fruit {} class Fruit { public Fruit(String name) { System.out.println("Fruit's constructor is invoked"); }}

Find out the errors in the program:

Will the Apple class compile?

Run: BJ_Apple_Fruit

Page 26: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

26

Check projects

• BJ_Apple_Fruit: Not compiled– Fruit: 1-arg constructor– Apple: no constructor

• BJ_Apple_Fruit2: Not compiled– Fruit: 1-arg constructor– Apple: 0-arg constructor

• BJ_Apple_Fruit3: compiled– Fruit: 0-arg constructor, 1-arg constructor– Apple: 0-arg constructor

• BJ_Apple_Fruit4: compiled– Fruit: 1-arg constructor– Apple: super(1-arg)

Page 27: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

27

Declaring a SubclassA subclass extends properties and methods from the superclass. You

can also:

Add new properties

Add new methods

Override the methods of the superclass

Question: Can we treat a subclass as a subset of its parent class?

Page 28: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

28

Overriding Methods in the Superclass

A subclass inherits methods from a superclass. Sometimes it is necessary for the subclass to modify the implementation of a method defined in the superclass. This is referred to as method overriding.

// Cylinder.java: New cylinder class that overrides the findArea()

// method defined in the circle class.

public class Cylinder extends Circle {

 /** Return the surface area of this cylinder. The formula is

* 2 * circle area + cylinder body area

*/

public double findArea() {

return 2 * super.findArea() + 2 * getRadius() * Math.PI * length;

}

// Other methods are omitted

}

Page 29: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

29

NOTE

•An instance method can be overridden only if it is accessible.

•private method m() in superclass

•private method m() in subclass cannot override the m() in superclass

•The two m() are unrelated

Page 30: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

30

Examples

• Class A and subclass B both contain method show()– Method_Overriding project

• Show() in B overrides show() in A

• How to call the show() method of A from B?

• To_Bypass_Overriding project• Show() in B calls show() in A by calling

super.show()

Page 31: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

31

NOTE

How about static methods in superclass?

•a static method can be inherited

•a static method cannot be overridden

•If you redefine a static method in a subclass, the method defined in the superclass is hidden.

•See staticMethodOverride.java

Page 32: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

32

Check:

• BJ_Method_Overriding

• BJ_Method_Overriding1

• BJ_Method_Overriding2

• Overriding_vs_Overloading

Page 33: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

33

The Object ClassAll Java classes descend from the java.lang.Object class.

If no inheritance is specified when a class is defined, the superclass of the class is Object.

public class Circle { ... }

Equivalent public class Circle extends Object { ... }

Page 34: 1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods

34

The toString() method in Object

The toString() method returns a string representation of the object. Default implementation returns:

a string with class name of the object is an instance, @, and a number representing this object. Ex:

Cylinder myCylinder = new Cylinder(5.0, 2.0);

System.out.println(myCylinder.toString());

displays something like Cylinder@15037e5.

- not very helpful or informative.

- should override the toString method to a more meaning string