Inheritance
Inheritance
O
D
U
L
Inheritance E
1
Inheritance
Inheritance is the procedure in which one class
inherits the attributes and methods of another class.
We can create a general class that defines traits
common to a set of related items. This class can then
be inherited by other, more specific classes, each
adding those things that are unique to it.
2
Inheritance
Superclass
A class that is inherited : Parent/Base Class
Subclass
The class that does the inheriting : Child/Derived Class
3
Inheritance
Basics
4
Example
5
Example
6
Example
7
Inheritance Basics
❏ The subclass B includes all of the members of its superclass, A.
❏ This is why subOb can access i and j and call showij( ).
❏ Also, inside sum( ), i and j can be referred to directly, as if they were part
of B.
❏ Even though A is a superclass for B, it is also a completely independent,
stand-alone class.
❏ Being a superclass for a subclass does not mean that the superclass
cannot be used by itself.
❏ A subclass can be a superclass for another subclass.
8
Inheritance Basics
❏ The general form of a class declaration that inherits a superclass is shown
here:
// body of class
❏ Can only specify one superclass for any subclass that you create.
❏ Java does not support the inheritance of multiple superclasses into a
single subclass.
9
Member Access
and
Inheritance
10
Member Access and
Inheritance
❏ Although a subclass includes all of the members of its superclass, it
cannot access those members of the superclass that have been declared as
private.
❏ In a class hierarchy, private members remain private to their class.
❏ Following program will not compile because the reference to j inside the
sum( ) method of B causes an access violation.
❏ Since j is declared as private, it is only accessible by other members
of its own class.
❏ Subclasses have no access to it.
11
Example
12
Using super
13
Using super
14
Using super
15
Using super to Call Superclass
Constructors
❏ A subclass can call a constructor method defined by its superclass by use
of the following form of super:
super(parameter-list);
16
Using super to Call Superclass
Constructors
17
Using super to Call Superclass
Constructors
❏ BoxWeight( ) calls super( ) with the parameters w, h, and d.
❏ This causes the Box( ) constructor to be called, which initializes width,
height, and depth using these values.
❏ BoxWeight no longer initializes these values itself.
❏ It only needs to initialize the value unique to it: weight.
❏ This leaves Box free to make these values private if desired.
❏ Since constructors can be overloaded, super( ) can be called using any
form defined by the superclass.
❏ The constructor executed will be the one that matches the arguments.
18
Using super to Call Superclass
Constructors
❏ When a subclass calls super( ), it is calling the constructor of its
immediate superclass.
❏ Thus, super( ) always refers to the superclass immediately above the
calling class.
❏ This is true even in a multileveled hierarchy.
❏ Also, super( ) must always be the first statement executed inside a
subclass constructor.
19
A Second Use for super
❏ The second form of super acts somewhat like this, except that it always
refers to the superclass of the subclass in which it is used.
super.member
20
Example
21
A Second Use for super
i in superclass: 1
i in subclass: 2
22
04
Creating a
Multilevel
Hierarchy
23
Creating a Multilevel
Hierarchy
❏ Can build hierarchies that contain as many layers of inheritance as we
like.
❏ It is perfectly acceptable to use a subclass as a superclass of another.
❏ Given three classes called A, B, and C.
❏ C can be a subclass of B, which is a subclass of A.
❏ Each subclass inherits all of the traits found in all of its superclasses.
❏ C inherits all aspects of B and A.
❏ subclass BoxWeight is used as a superclass to create the subclass called
Shipment.
❏ Shipment inherits all of the traits of BoxWeight and Box, and adds a field
called cost, which holds the cost of shipping such a parcel. 24
Example
25
Example
26
Example
27
Example
28
Creating a Multilevel
Hierarchy
29
Creating a Multilevel
Hierarchy
❏ Because of inheritance, Shipment can make use of the previously
defined classes of Box and BoxWeight, adding only the extra
information it needs for its own, specific application.
❏ This is part of the value of inheritance; it allows the reuse of code.
❏ super( ) always refers to the constructor in the closest superclass.
❏ The super( ) in Shipment calls the constructor in BoxWeight.
❏ The super( ) in BoxWeight calls the constructor in Box.
❏ If a superclass constructor requires parameters, then all subclasses must
pass those parameters “up the line.”
30
05
Method
Overriding
31
Method Overriding
❏ When a method in a subclass has the same name and type signature as a
method in its superclass, then the method in the subclass is said to
override the method in the superclass.
❏ When an overridden method is called from within a subclass, it will
always refer to the version of that method defined by the subclass.
❏ The version of the method defined by the superclass will be hidden.
32
Example
33
Example
Output:
k: 3
34
Method Overriding
35
Example
Output:
i and j: 1 2
k: 3
36
Method Overriding
void show() {
37
Method Overriding
❏ In B,
void show(String msg) {
System.out.println(msg + k);
}
❏ Calling from Override:
B subOb = new B(1, 2, 3);
subOb.show("This is k: "); // this calls show() in B
subOb.show(); // this calls show() in A
38
Method Overriding
❏ Output:
This is k: 3
i and j: 1 2
39
Dynamic Method Dispatch
40
Dynamic Method Dispatch
❏ It is the type of the object being referred to (not the type of the reference
variable) that determines which version of an overridden method will be
executed.
❏ If a superclass contains a method that is overridden by a subclass, then
when different types of objects are referred to through a superclass
reference variable, different versions of the method are executed.
❏ Example (refer textbook)
41
Why Overridden Methods?
42
Why Overridden Methods?
43
Example
44
Example
45
Applying Method Overriding
46
06
Using Abstract
Classes
47
Using Abstract Classes
49
Using Abstract Classes
❏ Any class that contains one or more abstract methods must also be
declared abstract.
❏ To declare a class abstract, you simply use the abstract keyword in front
of the class keyword at the beginning of the class declaration.
❏ There can be no objects of an abstract class.
❏ Any subclass of an abstract class must either implement all of the
abstract methods in the superclass, or be itself declared abstract.
50
Example
51
07
Using final
with
Inheritance
52
Using final with Inheritance
53
Using final to Prevent Overriding
54
Example
❏ Because meth( ) is declared
as final, it cannot be
overridden in B.
❏ If you attempt to do so, a
compile-time error will
result.
55
Using final to Prevent Inheritance
56
Using final to Prevent Inheritance
final class A {
// ...
// ...
}
57
T
Thank you!
H
A
N
K
Y
O
U
58