Lecture 07 - Inheritance 1
Lecture 07 - Inheritance 1
Programming
Lecture # 07: Inheritance
Muhammad Imran
(Based on Java, The Complete Reference)
http://www.secscourses.tk
1
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);
}
}
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;
17
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);
}
} 18
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.
19
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.
20
Using super
• super has two general forms:
The 1st calls the superclass constructor.
21
Using super to Invoke Superclass Constructor
22
Using super to Invoke Superclass Constructor
// BoxWeight now uses super to initialize its Box attributes.
class BoxWeight extends Box {
double weight; // weight of box
23
Using super to Invoke Superclass Constructor
arguments.
24
Using super to Invoke Superclass Constructor
31
Using super to Access Hidden
Members of Superclass
// Using super to overcome name hiding.
class A {
int i;
}
// Create a subclass by extending class A.
class B extends A {
int i; // this i hides the i in A
B(int a, int b) {
super.i = a; // i in A
i = b; // i in B
}
32
Using super to Access Hidden Members
of Superclass
void show() {
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
}
}
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);
subOb.show();
} i in superclass: 1
i in subclass: 2
}
33
Recommended Readings
• Page # 161 to 174, Chapter # 8: Inheritance from Herbert
Schildt, Java: The Complete Reference, J2SETM 9th Edition
34