Object Oriented Paradigm: Lecture # 8
Object Oriented Paradigm: Lecture # 8
Lecture # 8
Outline
Inheritance
A Superclass Variable can Reference a Subclass
Object
Using super
2
Inheritance
We can create a general class that defines traits
common to a set of related items
This class can then be inherited by others, more
specific classes, each adding those things that are
unique to it
A class that is inherited is called a superclass.
The class that does the inheriting is called a subclass.
Therefore, a subclass is a specialized version of a
superclass.
3
Inheritance
To inherit a class, we simply incorporate the definition of
one class into another by using the extends keyword.
The general form of a subclass declaration is:
class subclass-name extends superclass-name
{ /*body of class*/ }
We can specify only one superclass for any subclass.
This is because Java does not support multiple
inheritance.
4
Inheritance
// A simple example of inheritance.
// Create a superclass.
class A {
int i, j;
void showij() {
System.out.println("i and j: " + i + " " + j);
}
}
declared as private.
8
A More Practical Example
// This program uses inheritance to extend Box.
class Box {
double width;
double height;
double depth;
// construct clone of an object
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Program continues on next slide … 9
A More Practical Example
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
Program continues on next slide … 10
A More Practical Example
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
Or
SuperClass referenceVariable=subClassReference;
14
A Superclass Variable can Reference a
Subclass Object
class RefDemo {
public static void main(String args[]) {
BoxWeight weightbox = new BoxWeight(3, 5, 7, 8.37);
Box plainbox = new Box();
double vol;
vol = weightbox.volume();
System.out.println("Volume of weightbox is " + vol);
System.out.println("Weight of weightbox is " + weightbox.weight);
System.out.println();
plainbox = weightbox;
vol = plainbox.volume(); // OK, volume() defined in Box
System.out.println("Volume of plainbox is " + vol);
// System.out.println("Weight of plainbox is " + plainbox.weight);
}
15
}
A Superclass Variable can Reference a
Subclass Object
It is important to understand that it is the type of the
reference variable – not the type of the object that it
refers to – that determines what members can be
accessed.
16
Using super
BoxWeight(double w, double h, double d, double m) {
width = w; height = h;
depth = d; weight = m;
}
The subclass constructor explicitly initializes instance
variables of superclass.
– This duplicates the code found in superclass.
– It implies that a subclass must be granted access to these
members.
What if the superclass data is kept private?
super gives the solution. Whenever a subclass needs to
refer to its immediate superclass, it can do so by use of
the keyword super.
17
Using super
super has two general forms:
– The 1st calls the superclass constructor.
18
Using super to Invoke Superclass
Constructor
A subclass can call a constructor method defined
by its superclass by use of the following form of
super:
super(parameter-list);
19
Using super to Invoke Superclass
Constructor
// BoxWeight now uses super to initialize its Box attributes.
class BoxWeight extends Box {
double weight; // weight of box
superclass.
21
Using super to Invoke Superclass
Constructor
// A complete implementation of BoxWeight.
class Box {
private double width;
private double height;
private double depth;
31