Chapter Three Inheritance
Chapter Three Inheritance
Inheritance
1
Objectives
• To develop a subclass from a superclass through inheritance .
• To invoke the superclass’s constructors and methods using the super keyword.
• To override instance methods in the subclass .
• To distinguish differences between overriding and overloading .
• To explore the toString() method in the Object class .
• To discover polymorphism and dynamic binding .
• To describe casting and explain why explicit downcasting is necessary.
• To explore the equals() method in the Object class.
• To restrict access to data and methods to subclasses using the protected visibility
modifier .
• To prevent class extending and method overriding using the final modifier .
2
Introduction
• The three pillars of object-oriented programming
are
1. Encapsulation,
2. Inheritance, and
3. Polymorphism.
3
Inheritance
8
Superclasses and Subclasses
9
Example 1: GeometricObject Superclass
Import java.util.Date;
public class GeometricObject {
private String color = "white";
private boolean filled;
private Date dateCreated;
/** Construct a default geometric object */
public GeometricObject() {
dateCreated = new Date();
}
// Construct a geometric object with the specified color and filled value
public Circle() {
}
15
Rectangle Subclass
public class Rectangle extends GeometricObject {
public Rectangle() {
}
18
Rectangle Subclass
19
TestCircleRectangle Class
public class TestCircleRectangle { inherited from
public static void main(String[] args) { superclass.
23
Solution: Inheritance
• Inheritance allows you to write new classes that
inherit from existing classes
using
inheritance
superclass
Animal
subclass
String name
subclass String getName()
Dog Cat
int fleas int hairballs
int getFleas() int getHairballs()
void speak() void speak() 25
Animal Superclass
public class Animal {
public Animal(String n) {
name = n;
}
26
Dog Subclass
public class Dog extends Animal {
29
Using the super Keyword
• 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.
– Java requires that the statement that uses the keyword super
appear first in the constructor. 30
Using the super Keyword
1. Calling Superclass Constructors
– The syntax to call a superclass’s constructor is:
super(), or super(parameters);
– The statement super() invokes the no-arg constructor of its
superclass, and the statement super(arguments) invokes the
superclass constructor that matches the arguments.
– The statement super() or super(arguments) must appear in
the first line of the subclass’s constructor; this is the only way
to explicitly invoke a superclass constructor.
– For example:
2. Constructor Chaining
– A constructor may invoke an overloaded constructor or its
superclass’s constructor.
– If none of them is invoked explicitly, the compiler automatically
puts super() as the first statement in the constructor. For example:
33
Calling Superclass Methods
• The keyword super can also be used to reference a method other than
the constructor in the superclass. The syntax is:
super.method(parameters);
• You could rewrite the printCircle() method in the Circle class as
follows:
36
The protected Data Field and Methods
This Figure illustrates how a
public, protected, default, and
private data field or method in
class C1 can be accessed
from a class C2 in the same
package, from a subclass C3
in the same package, from a
subclass C4 in a different
package, and from a class C5
in a different package.
37
The protected Data Field and Methods
• Use the private modifier to hide the members of the class
completely so that they cannot be accessed directly from outside the
class.
39
NOTE
42
Overriding vs. Overloading
43
Overriding vs. Overloading
• Note:
– Overridden methods are in different classes related by
inheritance; overloaded methods can be either in the
same class or different classes related by inheritance.
44
Overriding vs. Overloading
@Override
public String toString() {
return super.toString()+"\nradius is " + radius;
}
}
45
Is-a Versus Has-a Relationships
• Is-a represents the inheritance/extends
• Has-a represents composition/aggregation
• Example: Dog is an Animal (inheritance)
Dog has an Eyes (composition)
House is a Building (inheritance)
House has a door (composition)
Person
is a is a
Student Teacher
has a has a
BirthDate 46
Object Composition
48
Class Representation
• An aggregation relationship is usually represented as a data
field in the aggregating class.
• For example, the relationship in the following Figure can be
represented as follows:
49
Aggregation or Composition
• Since aggregation and composition relationships
are represented using classes in similar ways,
many texts don’t differentiate them and call both
compositions.
• Aggregation: “is part of”
• Composition: “is entirely made up of”
• stronger version of aggregation
• the parts live and die with the whole
50
Aggregation Between Same Class
• Aggregation may exist between objects of the same
class. For example, a person may have a supervisor.
1
Person
Supervisor
1
1
Person
Supervisor
m
52
The Object Class and Its Methods
Every class in Java is descended from the java.lang.Object
class. If no inheritance is specified when a class is defined,
the superclass of the class is Object by default. For
example, the following two class definitions are the same:
54