8_Inheritance
8_Inheritance
Inheritance and
Interfaces
Inheritance
2
Inheritance is one of the cornerstones of object-oriented
programming because it allows the creation of hierarchical
classifications.
Using inheritance, you 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.
In this a class that is inherited is called a superclass in the
terminology of Java,
The class that does the inheriting is called a subclass. Therefore,
a subclass is a specialized version of a superclass. It inherits all of
the instance variables and methods defined by the superclass and
adds its own elements.
Inheritance Basics…. 3
super(arg-list);
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.
This usage has the following general form:
super.member;
{
int i;
}
// Create a subclass by extending class A.
class B extends A class UseSuper
{ {
int i; // this i hides the i in A public static void main(String args[ ])
B(int a, int b) {
{ B subOb = new B(1, 2);
super.i = a; // i in A subOb.show();
i = b; // i in B }
} }
void show()
{
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
}
}
21
class Room
{
int length, breadth; 22
Room(int x, int y)
{
length = x;
breadth = y;
}
int area()
{
return ( length * breadth );
}
}
class Hall extends Room Always the first statement to
{ be executed in the subclass
int height;
Hall(int x, int y, int z) constructor.
{
super(x, y);
height = z;
}
int volume()
{
return (length * breadth * height);
}
}
class InherTest
{ 23
}
}
class A
{ 24
int i;
}
class B extends A
{
int i; // this i hides the i declared in A
B(int a, int b)
{
super.i = a; // i in A
i = b; // i in B
}
void show()
{
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
}
}
class UseSuper
25
{
public static void main(String args[])
{
B subOb = new B(1, 2);
subOb.show();
}
}
Creating a Multilevel Hierarchy….. 26
class MultiLevel
{
public static void main(String args[])
{
C obj = new C();
obj.i=1;
obj.j=2;
obj.k=3;
obj.sum();
}
}
When Are Constructors Called….. 29
int k;
void show() {
super.show(); // this calls A's show()
System.out.println("k: " + k);
}
}
class Override
{ 37
}
Using final with Inheritance 46
{
System.out.println("Illegal!");
}
}
Using final to Prevent Inheritance ….
49
• Sometimes to prevent a class from being inherited.
}
// The following class is illegal.
class B extends A{// ERROR! Can't subclass A
// ...
}
As the comments imply, it is illegal for B to inherit A since A is declared as final
Local Variable Type Inference and
Inheritance 50
class MyClass
{
// ...
}
class FirstDerivedClass extends MyClass
{
int x;
// ...
}
class SecondDerivedClass extends FirstDerivedClass
{
int y;
// ...
}
Local Variable Type Inference and
Inheritance 52
class TypeInferenceAndInheritance
{
// Return some type of MyClass object.
static MyClass getObj(int which) {
switch(which) {
case 0: return new MyClass();
case 1: return new FirstDerivedClass();
default: return new SecondDerivedClass();
}
}
Local Variable Type Inference and
Inheritance 53