Lecture 9
Lecture 9
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 1 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 2
rights reserved. 0132130807 rights reserved. 0132130807
•
+setColor(color: String): void Sets a new color.
+isFilled(): boolean Returns the filled property. Explicitly using the super keyword.
+setFilled(filled: boolean): void Sets a new filled property.
+getDateCreated(): java.util.Date
+toString(): String
Returns the dateCreated.
Returns a string representation of this object.
• A constructor is used to construct an instance of a class.
• Unlike properties and methods, a superclass's constructors are
Circle Rectangle
-radius: double -width: double not inherited in the subclass.
+Circle() -height: double
+Circle(radius: double)
+Circle(radius: double, color: String,
+Rectangle()
+Rectangle(width: double, height: double)
• They can only be invoked from the subclasses' constructors,
filled: boolean)
+getRadius(): double
+Rectangle(width: double, height: double
color: String, filled: boolean) using the keyword super.
+setRadius(radius: double): void +getWidth(): double
+getArea(): double
+getPerimeter(): double
+setWidth(width: double): void
+getHeight(): double • If the keyword super is not explicitly used, the superclass's
+getDiameter(): double
+printCircle(): void
+setHeight(height: double): void
+getArea(): double
no-arg constructor is automatically invoked.
+getPerimeter(): double
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 3 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 4
rights reserved. 0132130807 rights reserved. 0132130807
Superclass’s Constructor Is Always Invoked
Using the Keyword super
A constructor may invoke an overloaded constructor or its
superclass’s constructor. If none of them is invoked The keyword super refers to the superclass
explicitly, the compiler puts super() as the first statement of the class in which super appears. This
in the constructor. For example, keyword can be used in two ways:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 5 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 6
rights reserved. 0132130807 rights reserved. 0132130807
Constructor Chaining
CAUTION Constructing an instance of a class invokes all the superclasses’ constructors
along the inheritance chain. This is called constructor chaining.
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
• You must use the keyword super to call public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
the superclass constructor. }
}
public Employee(String s) {
• Java requires that the statement that uses }
System.out.println(s);
}
the keyword super appear first in the class Person {
constructor. public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 7 } Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 8
rights reserved. 0132130807 rights reserved. 0132130807
Trace Execution Trace Execution
public class Faculty extends Employee { public class Faculty extends Employee {
public static void main(String[] args) { public static void main(String[] args) {
new Faculty(); 1. Start from the new Faculty(); 2. Invoke Faculty
} main method } constructor
public Faculty() { public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked"); System.out.println("(4) Faculty's no-arg constructor is invoked");
} }
} }
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 17 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 18
rights reserved. 0132130807 rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 19 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 20
rights reserved. 0132130807 rights reserved. 0132130807
Overriding Methods in the Superclass NOTE
• A subclass inherits methods from a superclass.
• Like an instance method, a static method
• Sometimes it is necessary for the subclass to modify the
implementation of a method defined in the superclass. can be inherited.
• This is referred to as method overriding. • However, a static method cannot be
overridden.
public class Circle extends GeometricObject {
// Other methods are omitted
• If a static method defined in the
/** Override the toString method defined in GeometricObject */ superclass is redefined in a subclass, the
public String toString() {
return super.toString() + "\nradius is " + radius; method defined in the superclass is
}
}
hidden.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 21 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 22
rights reserved. 0132130807 rights reserved. 0132130807
} }
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 25 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 26
rights reserved. 0132130807 rights reserved. 0132130807