Chapter 6 - Part 1
Chapter 6 - Part 1
Object-Oriented Programming
Chapter 6 – Part 1
A scenario (1)
Suppose you have created the following 3 classes for an application:
Circle Rectangle Triangle
-color: String -color: String -color: String
-radius: double -length: double -base: double
-width: double -height: double
+getColor(): String +getColor(): String +getColor(): String
+setColor(color:String): void +setColor(color:String): void +setColor(color:String): void
+getRadius(): double +getLength(): double +getBase(): double
+setRadius(radius: double): void +setLength(length: double): void +setBase(base: double): void
+getArea(): double +getWidth(): double +getHeight(): double
+setWidth(width: double): void +setHeight(height: double): void
+getArea(): double +getArea(): double
▪ If the client now wants to record the date created of each Circle, Rectangle and
Triangle object, what would you do?
▪ A few weeks later, the client informs you that he also wants the fill status of each
Circle, Rectangle and Triangle object to be recorded …
What would you do ?
A scenario (2)
Circle Rectangle Triangle
-color: String -color: String -color: String
-radius: double -length: double -base: double
-width: double -height: double
+getColor(): String +getColor(): String +getColor(): String
+setColor(color:String): void +setColor(color:String): void +setColor(color:String): void
+getRadius(): double +getLength(): double +getBase(): double
+setRadius(radius: double): void +setLength(length: double): void +setBase(base: double): void
+getArea(): double +getWidth(): double +getHeight(): double
+setWidth(width: double): void +setHeight(height: double): void
+getArea(): double +getArea(): double
Inherits
C2 Saving Checking
account account Subclass
Term Aliases
Subclass Child Class, Extended Class or Derived Class
Superclass Parent Class, Base Class
Refer to the following classes
under folder Demo 6.1:
▪ GeometricObject
▪ Circle
▪ Rectangle
▪ TestCircleRectangle
Superclasses and Subclasses (3)
■Java supports inheritance by using the extends keyword.
■Thus, the subclass adds to (extends) the superclass. This is an
important and powerful concept in object-oriented
programming.
■Example:
Circle Rectangle
Cylinder
Rationale for using inheritance (2)
■ Class hierarchy
■Inheritance allows you to build a hierarchy among classes.
■Example:-
■A general GeometricObject class is used to define general variables
of a geometric object (e.g. object color) and methods (e.g.
setColor()) that are common to all geometric objects.
■The Circle class and Rectangle class which represents specific
geometric objects can both be derived from the GeometricObject
class
GeometricObject
Circle Rectangle
Note
■A subclass is not a subset of its
superclass. GeometricObject
■Rather, a subclass is an extension of its
superclass as it contains more information
and functions than its superclass.
Circle Rectangle
■Inheritance is used to model the is-a
relationship.
■Hence, a subclass and its superclass must
have the is-a relationship.
■In Java, multiple inheritance is not
Superclass1 Superclass 2
allowed - only single inheritance is
allowed.
subclass
4. Superclass Constructors
▪Superclass’ constructors are not inherited by its subclasses.
▪The superclasses and subclasses have their own constructors
(whether default or explicit).
▪ The constructor for the superclass constructs the superclass
portion of the object whereas the constructors for the subclass
constructs the subclass part.
Invoking superclass’s constructor
▪ A superclass’s constructors will be invoked in its subclasses’ constructors
▪ They are invoked explicitly or implicitly.
▪ Explicitly using the super keyword.
▪ If the keyword super is not explicitly used, the superclass's no-arg constructor
is automatically invoked.
public class Circle extends GeometricObject {
import java.util.Date; private double radius;
public class GeometricObject {
private String color; public Circle(String c, double r){
private Date dateCreated; super(c);
public GeometricObject(){ radius = r;
this(“Blue”); }
} public Circle(){
public GeometricObject(String c) { radius = 1.0;
color = c; }
dateCreated = new Date();
}
….
}
Superclass’s Constructor Is Always Invoked
▪ If none of the constructor is invoked explicitly, the compiler puts
super() as the first statement in the constructor.
Using the Keyword super
■ The keyword super refers to the superclass of the
class in which super appears.
■ This keyword can be used in two ways:
■To call a superclass constructor
■To call a superclass method
Calling a Superclass’ Constructor
■Syntax to call a superclass constructor:
super(arguments);
■The statement super(arguments)must always be the first
statement executed inside a subclass constructor.
Caution
⮚You must use the keyword super to call the
superclass constructor.
⮚Invoking a superclass constructor’s name in a
subclass causes a syntax error.
5. Constructor Chaining
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
} Person
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
public Employee(String s) {
Faculty
System.out.println(s);
} Constructing an instance of a class
}
invokes all the superclasses’ constructors
class Person {
public Person() {
along the inheritance chain. This is called
System.out.println("(1) Person's no-arg constructor is constructor chaining.
invoked");
}
}
animation
Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty(); 1. Start from the
} main method
public Faculty() {
System.out.println("(4) Faculty'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");
}
}
animation
Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty(); 2. Invoke Faculty
} constructor
public Faculty() {
System.out.println("(4) Faculty'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");
}
}
animation
Trace Execution
public 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");
}
}
3. Invoke Employee’s no-
class Employee extends Person { arg constructor
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");
}
}
animation
Trace Execution
public 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");
}
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
animation
Trace Execution
public 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");
}
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
5. Invoke Person() constructor
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
animation
Trace Execution
public 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");
}
}
public Employee(String s) {
System.out.println(s);
}
}
6. Execute println
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
animation
Trace Execution
public 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");
}
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
7. Execute println
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
animation
Trace Execution
public 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");
}
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
8. Execute println
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
animation
Trace Execution
public 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");
}
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Constructor Chaining Example
■Refer to the following classes under folder Demo 6.2:
■GeometricObject
■Circle
■Rectangle
■TestCircleRectangle
Example on the Impact of a Superclass without
no-arg Constructor
Find out the errors in the program:
public class Apple extends Fruit {
}
class Fruit {
public Fruit(String name) {
System.out.println("Fruit's constructor is invoked");
}
}
Design Guide:
▪It is better to provide a no-arg constructor for every class to ensure the
class is easy to extend and to avoid errors.
6. Overriding Methods in the Superclass (1)
▪ A subclass inherits methods from a superclass. A
subclass extends properties and methods from
the superclass.
is equivalent to invoking
System.out.println(circle.toString())
The toString method (3)
■Refer to the following classes in the Demo 6.4 folder to
see how the toString() method is overridden:
■GeometricObject
■Circle
■Rectangle
■TestCircleRectangle
7.2 The equals Method
■The equals() method compares the contents of two objects.
■The default implementation of the equals method in the Object class is
as follows:
GeometricObject
Circle Rectangle
Method Overriding (2)
▪ An instance method can be overridden only if it is accessible.
▪ Thus a private method cannot be overridden, because it is not accessible outside
its own class.
▪ If a method defined in a subclass is private in its superclass, the two methods are
completely unrelated.
public class GeometricObject {
private void display() {
System.out.println("\nColor: " + color + "\nFilled: " +
filled + "\nDate created: " + dateCreated);
}
}
public class Circle extends GeometricObject {
……………………
public void display() {
super.display(); //error
System.out.println(“Radius: " + radius);
}
}
Method Overriding (3)
Method overriding occurs ONLY when the return types and signatures of the
two methods are identical. If they are not, then the two methods are simply
overloaded.
To Do
■Review the slides and source code for this chapter.
■Read up the relevant portions of the recommended text.
■Do the tutorial and complete the remaining practical
questions for this chapter.
■We shall selectively discuss them during class.