What Is Inheritance?
What Is Inheritance?
Inheritance
What is Inheritance?
A process by which a class can extend another class, inheriting all its
variables,
methods,
while redefining (overriding) some of them,
and/or adding its own.
Thus inheritance promotes code re-use. If a class “B” needs to define some behavior and
a lot of that behavior is pre-defined in any other class “A” then instead of defining the
entire behavior again, the class “B” should inherit from that class “A”.
An Example
Inheritance allows classes to be defined in terms of other classes. For example, mountain
bikes, road bikes, and tandems are all kinds of bicycles. In object-oriented terminology,
mountain bikes, road bikes, and tandems are all subclasses of the bicycle class. Similarly,
the bicycle class is the superclass of mountain bikes, road bikes, and tandems. This
relationship is shown in the following figure.
-1-
Tree Hierarchy
Inheritance leads to hierarchy of classes. The classes arranged in tree hierarchy increases
the understanding. For example consider the following tree hierarchy
Person
Employee
Teacher Manager
Superclass
- A superclass is the class above in the hierarchy. For example Person is a direct
superclass of Employee and indirect superclass of Teacher or manager.
Subclass
- A subclass is the class below (of super class) in the hierarchy. For example,
Employee is a direct subclass of Person; also teacher is indirect subclass of
Person but direct subclass of Employee.
-2-
two sets of handle bars; some mountain bikes have an additional chain ring,
giving them a lower gear ratio.
Sibling
- Two children (subclasses) of the same parent class are called siblings. In the
above example, Teacher and Manager are siblings.
Considering the above class hierarchy, we can say that Teacher is an Employee and
rightly so as teacher is an employee from the university perspective. Also Manger is an
Employee & Employee is a Person.
-3-
Inheritance Warning
Inheritance is the core part of OOP. Inheritance is a clever and appealing concept.
However using it properly needs clarity of concept. A common error made by beginner
OOP programmers is that they do not make use of inheritance. Let’s make our concepts
clear from the following example
Horse/Zebra Example
With inheritance, we define classes in terms of other classes. This can be a great
shortcut if the classes are similar
.
Suppose you have the hierarchy of all the animal classes, except the zebra class.
- Option 2 - Locate the horse class. Make Zebra class a subclass of Horse class
Benefits – Zebra inherits 90% of its behavior so NO coding requirement + time save.
In the Zebra class, define the few things that are features of Zebras but not horses.
What would you do if horses have some features that Zebras don’t have
- You’ll see if the parent class of horses can do the job. Then you will inherit
from the parent class of horse and not horse itself.
Points to Remember
Java doesn’t support multiple inheritances, it only support single inheritance. It means
that a class can only extend from one class at one time.
Things declared “private” are not directly accessible to subclass or any other class.
-4-
Whenever you construct the object of subclass, subclass constructor would be called
as well as superclass constructor would also be called.
- If you don’t specify which superclass constructor to be called, the java will
automatically call the default constructor of the superclass. (Example of these are
coming up shortly).
If you don’t write any constructor (no argument or parameterize) of subclass than
java will write default constructor for you.
Method Overriding
The process of redefining a method, that is already defined in the superclass with
same signature (name + parameters) and return type, in the subclass is called method
overriding.
Suppose that area method is defined in the Shape class and Circle class is a subclass
of Shape class. So, area method through inheritance is available to Circle class but it
is useless for Circle class since the area calculating methodology of circle (חr2) is
different of Shape. As a result, Circle class has to redefine area method (overriding)
to achieve the desired behavior.
-5-
Inheritance using Java
Keyword extends
Keyword extends is used for defining inheritance between super and sub class as the
subclass is extending the behavior and functionality of superclass.
……………………………
Keyword super
The keyword super is used for two purposes:
To call the superclasss method from subclass that is overridden in the subclass
Both usage of the super keyword is shown in the coming up code example.
Code Example
Employee.java
The Employee class has name and id as instance variables. Two constructors, default &
parameterized are also provided. Other methods include setter/getters, display (capable of
printing employee object on console) and toString method (capable of converting
employee object into string) is overridden here.
Note: The toString() method is actually defined in the Object class, and every class in
java inherits from Object class whether extends kewords is specified or not
class Employee{
//parameterized constructor
public Employee(int id, String name){
this.id = id;
this.name = name;
}
-6-
//default constructor
public Employee(){
this (10, “not set”);
}
//setters
public void setId (int id) {
this.id = id;
}
//getters
public int getId () {
return id;
}
}//end class
Consider the following line of code in the default constructor of the Employee’s class.
As you already know, this is the keyword and here it is used to call the Employee class
parameterize constructor. Hence, Keyword this can be used to call the other constructor
of the same class. For example, to call the default constructor, write following code:
this ( );
-7-
Teacher.java
The Teacher class extends from Employee class. The teacher class has an additional
attribute i.e. qualification. Two constructors, default & parameterized are also defined.
getter/setter is also defined for qualification. Display & toString methods are overridden
in this class.
//default constructor
public Teacher () {
//implicit call to superclass default construct
qual = "";
}
//parameterized constructor
public Teacher(int i, String n, String q){
//setter
public void setQual (String qual){
this.qual = qual;
}
//getter
public String getQual(){
return qual;
}
-8-
The first usage of keyword super is to call superclass’s constructors. This has been shown
in the parameterize constructor of Teacher class to make call to parameterize constructor
of the parent class i.e. Employee class.
Note that we didn’t use the super keyword in the default constructor of Teacher class but
java will implicitly call the default (no argument) constructor of the Employee class.
The second usage of super is to call the overridden methods. This has been shown in the
display and toString method of the Teacher class.
Points to Remember
The call to superclass constructor via keyword super must be the first line (as used in
the parameterized constructor) or otherwise compiler will raise an error.
If the method is not overridden in the subclass than there is no need to use the
keyword super before method name.
Test.java
This class acts as a driver class as it contains the main method. Objects of Employee &
Teacher class are created inside main and calls are made to display and toString method
using these objects.
class Test{
} //end of main
}//end class
-9-
Object Class
A class called Object is defined in the java.lang package of the Java standard
class library
All classes, by default, are derived from the Object class i.e. if a class is not
explicitly defined to be the child of any superclass class, java makes it the child of the
Object class
Object class contains some useful methods like toString(), equals() (for comparing
references) etc.
- As we didn’t specify the superclass of the Employee class. So, java made
Object class its superclass. That’s why we have overridden toString
method in the Employee class.
The Object class is therefore the ultimate root of all class hierarchies.
The one hierarchy we discussed is modified as one given below assuming that Employee
class inherits from Person class. Hence, the Object class will became the superclass of the
Person class (ultimate root class).
Object
Person
Employe
Teacher Manager
- 10 -
References:
- 11 -