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
super(); // or super(parameters);
p.404 Figure Constructor Chaining
參考 p.405 程式 Listing 注意輸出的順序(物件產生的順序)
p.406 Caution. 最好提供 no-arg constructor
super.method(parameters);
Overriding Methods: subclass to modify the implementation of a method defined in the superclass
method signature
public class ClassName {} public class ClassName extends Object {}
Invoking toString() on an object returns a string that describes the object.
Polymorphism: An object of a subclass can be used wherever its superclass object is used.
參考 p.409 程式 Listing 11.5 PolymorphismDemo.java
// 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
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
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
p.414 Figure 11.3 An ArrayList stores an unlimited number of objects.
參考 p.414 程式 Listing 11.8 TestArrayList.java
p.416 Note. Recompile with –Xlint:unchecked
重要 p.417 Table 11.1 Differences and Similarities between Arrays and ArrayList
p.417 Figure 11.4 UML for MyStack class
參考 p.417 程式 Listing 11.9 MyStack.java
重要 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% )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() { // ... } }