ch11 INHERITANCE AND POLYMORPHISM

11-1 Introduction

Our focus here is on class design

11-2 Superclasses and Subclasses

p.398 Figure 11.1 The GeometricObject class is the superclass for Circle and Rectangle.

參考 p.399 程式 Listing 11.1 GeometricObject1.java

參考 p.400 程式 Listing 11.2 Circle4.java

public Subclass extends Superclass

參考 p.402 程式 Listing 11.3 Rectangle1.java

參考 p.403 程式 Listing 11.4 TestCircleRectangle.java

11-3 Using the super Keyword

11-3-1 Calling Superclass Constructors

super(); // or 
super(parameters);

11-3-2 Constructor Chaining

p.404 Figure Constructor Chaining

參考 p.405 程式 Listing 注意輸出的順序(物件產生的順序)

p.406 Caution. 最好提供 no-arg constructor

11-3-3 Calling Superclass Methods

super.method(parameters);

11-4 Overriding Methods

Overriding Methods: subclass to modify the implementation of a method defined in the superclass

  1. private method: An instance method can be overridden only if it is accessible
  2. static method: hidden

11-5 Overriding vs. Overloading

method signature

11-6 The Object Class and Its toString() Method

public class ClassName {}
public class ClassName extends Object {}

Invoking toString() on an object returns a string that describes the object.

11-7 Polymorphism

Polymorphism: An object of a subclass can be used wherever its superclass object is used.

參考 p.409 程式 Listing 11.5 PolymorphismDemo.java

11-8 Dynamic Binding

// which toString? Object, or GeometricObject
Object o = new GeometricObject();
System.out.println(o.toString()); 

Which toString() method is invoked by o is determined by o’s actual type. This is known as dynamic binding

p.410 Figure 11.2 The method to be invoked is dynamically bound at runtime

參考 p.410 程式 Listing 11.6 DynamicBindingDemo.java

11-9 Casting Objects and the instanceof Operator

Object o = new Student(); // Implicit casting
Student b = (Student) o; // Explicit casting

if (o instanceof Student) {
	((Student) o).getScore(); // casting, 注意刮號
}

參考 p.4 程式 Listing 11.7 CastingDemo.java

11-10 The Object’s equals Method

tests whether two objects are equal

// default implementation
public boolean equals(Object obj) {
	return (this == obj);
}

// Circle class implementation
public boolean equals(Object o) {
if (o instanceof Circle) {
	return radius == ((Circle)o).radius;
} else {
	return false;
}

重要 p.4 Note. == two objects have the same references

11-11 好用的 The ArrayList Class

p.414 Figure 11.3 An ArrayList stores an unlimited number of objects.

參考 p.414 程式 Listing 11.8 TestArrayList.java

  1. add(Object)
  2. size()
  3. add(index, Object)
  4. remove(Object)
  5. remove(index)
  6. toString()
  7. getIndex()
  8. set(index, Object)
  9. clear()

p.416 Note. Recompile with –Xlint:unchecked

重要 p.417 Table 11.1 Differences and Similarities between Arrays and ArrayList

11-12 A Custom Stack Class

p.417 Figure 11.4 UML for MyStack class

參考 p.417 程式 Listing 11.9 MyStack.java

11-13 The protected Data and Methods

重要 p.419 Table 11.2 Data and Methods Visibility

重要 p.419 Figure 11.5 Visibility modifiers are used to control how data and methods are accessed.

(Table 11.2 and Figure 11.5 觀念出題機率 90% )

11-14 Preventing Extending and Overriding

use the final modifier to indicate that a class is final and cannot be a parent class

public final class C {
// Data fields, constructors, and methods omitted
}

public class Test {
	// Data fields, constructors, and methods omitted
	public final void m() {
		// ...
	}
}