Types of Inheritance
There exists basically three types of inheritance.
- Multilevel inheritance
- Multiple inheritance
- Hierarchical inheritance
1. In single inheritance, one class extends one class only. In multilevel inheritance, the ladder of single inheritance increases.
2. In multiple inheritance, one class directly extends more than one class.
3. In hierarchical inheritance one class is extended by more than one class.
For a fresher, it will be very confusing, no doubt. Let us go in detail of each one programmatically. Remember, to learn Java, C++ knowledge is not at all required. In fact, you learn OOPs concepts through C++ syntax in C++ and through Java syntax in Java. That is all.
1. Multilevel Inheritance
In multilevel, one-to-one ladder increases. Multiple classes are involved in inheritance, but one class extends only one. The lowermost subclass can make use of all its super classes' members. Multilevel inheritance is an indirect way of implementing multiple inheritance. Following program explains.
class Aves
{
public void nature()
{
System.out.println("Generally, Aves fly");
}
}
class Bird extends Aves
{
public void eat()
{
System.out.println("Eats to live");
}
}
public class Parrot extends Bird
{
public void food()
{
System.out.println("Parrot eats seeds and fruits");
}
public static void main(String args[])
{
Parrot p1 = new Parrot();
p1.food(); // calling its own
p1.eat(); // calling super class Bird method
p1.nature(); // calling super class Aves method
}
}
Now, Parrot has two super classes Bird and Aves; but extended one-to-one. Parrot as a subclass can make use of the methods of Bird and Aves. Bird can make use of the methods of Aves only, but Aves as a super class cannot access subclass members. Following figure gives a schematic representation of the multilevel hierarchy with the classes involved in the program.
2. Multiple Inheritance
In multiple inheritance, one class extends multiple classes. Java does not support multiple inheritance but C++ supports. The above program can be modified to illustrate multiple inheritance. The following program does not work.
calss Aves { }
class Bird { }
public class Parrot extends Aves, Bird { }
In the above code, Parrot extends both Aves and Bird. This is not supported by Java and the above code raises compilation error. Following is the schematic representation.
Note: Java supports multiple inheritance partially through interfaces.
3. Hierarchical Inheritance
In hierarchical type of inheritance, one class is extended by many subclasses. It is one-to-many relationship. A realtime example is available at dynamic binding.
class Aves
{
public void fly()
{
System.out.println("Generally, aves fly");
}
}
class Parrot extends Aves
{
public void eat()
{
System.out.println("Parrot eats fruits and seeds");
}
}
class Vulture extends Aves
{
public void vision()
{
System.out.println("Vulture can see from high altitudes");
}
}
public class FlyingCreatures
{
public static void main(String args[])
{ // all the following code is composition for FlyingCreatures
Parrot p1 = new Parrot();
p1.eat(); // calling its own member
p1.fly();
// calling super class member by inheritance
Vulture v1 = new Vulture();
v1.vision(); // calling its own member
v1.fly(); // calling super class member by inheritance
}
}
In the above code, Aves class is extended by two classes – Parrot and Vulture. Both classes can make use of the methods of Aves. Even though the Parrot and Vulture are subclasses of the same class Aves, but still they cannot make use of each other members. Parrot and Vulture are known as "siblings". Siblings are disjoint and they cannot make use of other members as between them no inheritance is involved (like two sons of a father; one son's property cannot be shared by other but both can share the property of father). Following is the schematic representation of the classes involved.
Dynamic binding and dynamic polymorphism use hierarchical inheritance.
Disadvantages of Inheritance
- Both classes (super and subclasses) are tightly-coupled.
- As they are tightly coupled (binded each other strongly with extends keyword), they cannot work independently of each other.
- Changing the code in super class method also affects the subclass functionality.
- If super class method is deleted, the code may not work as subclass may call the super class method with super keyword. Now subclass method behaves independently.