DEV Community

221910303035
221910303035

Posted on

Types of Inheritance

Types Of Inheritance In Java
Depending on the way the classes are inherited and how many classes are inherited, we have the following types of inheritance as shown in the below figure.

types of inheritance

As shown in the above figure, there are five types of inheritances in Object-Oriented programming as described below:

1) Single Inheritance: When a derived class or subclass inherits from only one base or superclass then it is single inheritance.

2) Multilevel Inheritance: In Multilevel Inheritance, we have more than one level wherein a class inherits from a base class and the derived class in turn is inherited by another class.

3) Hierarchical Inheritance: An inheritance hierarchy is formed in this type of inheritance when a superclass is inherited by more than one class.

4) Multiple Inheritance: Multiple inheritance is the one in which a class can inherit properties and behavior from more than one parent.

5) Hybrid Inheritance: When one or more types of inheritance are combined, then it becomes a hybrid inheritance.

Note that Java supports only single, multilevel, and hierarchical type of inheritance using classes. Java does not support multiple and hybrid inheritance with classes.

Now we will discuss each type of inheritance in detail with programming examples.

Single Inheritance In Java
A single inheritance is depicted as shown below:

Single inheritance in Java

Here a subclass inherits from a single superclass. This is single inheritance. Any animal like Dog inherits from the Animal species. This is the simplest form of inheritance.

Given below is a Java program that explains Single Inheritance.

//base class:Animal
class Animal
{

void Action_eat()
{
System.out.print("eating...");
}

}

//derived class:Dog
class Dog extends Animal
{

void Action_bark()
{
System.out.print("barking...");

}  
Enter fullscreen mode Exit fullscreen mode

}

class Main{

public static void main(String args[]){

Dog d=new Dog(); //create an object of derived class

System.out.print("The dog is ");
d.Action_bark(); //call derived class method
System.out.print("\nThe dog is ");
d.Action_eat(); //call base class method
}
}

Output:

single inheritance example

Here, we have one method eat (Action_eat) in base class Animal which is common to Animal species. We have a derived class Dog that derives from Animal class. In the Dog class, we have a method specific to Dog species, bark (Action_bark).

Then we create a Dog object in the main method and as the Dog class has inherited the Animal class, this object can call eat as well as bark method.

Multilevel Inheritance In Java
In multi-level inheritance, we have a chain of inheritance. This means that we have a parent class that is inherited by a derived class. Derived class in turn acts as a parent to another class and so on.

The multi-level inheritance can be represented as below:

multilevel inheritance

As seen in the above figure there is a parent class A. Class B inherits from Class A. Then there is another class C that in turn inherits from Class B. Thus we can see that it forms a chain of inheritance. Here class B becomes an intermediary class that connects classes A and C.

Continuing with our Animal class example below, we can have a Dog class derived from Animal class. Then we can have another class Puppy which is a baby dog derived from Dog class. This way, we can have the multilevel inheritance.

An example program for Multilevel Inheritance is shown below.

import java.util.;
import java.lang.
;
import java.io.*;
//parent class A

class A
{
public void print_A()
{
System.out.print("SoftwareTestingHelp ");
}
}
//Derived class B - intermediary

class B extends A
{
public void print_B()
{
System.out.print("Java Series ");
}
}
//Derived Class C

class C extends B
{
public void print_C()
{
System.out.print("Tutorials");
}
}

public class Main
{
public static void main(String[] args)
{
C c_obj = new C(); //create Class C obj
c_obj.print_A(); //call grandparent class method
c_obj.print_B(); //call parent class method
c_obj.print_C(); //call member method
}
}
Output:

Multilevel inheritance

We have programmed the exact chain shown above. Then in the main method, we create an object of class C. The class C object then can access the methods of its parent B as well as grandparent A.

Hierarchical Inheritance In Java
A class can have more than one class derived from it. So we have one base or superclass and more than one subclasses. This type of inheritance is called “Hierarchical inheritance”.

The hierarchical inheritance is diagrammatically represented below:

Hierarchical inheritance in Java

As an example of hierarchical inheritance, we can represent Animal class as a superclass and then have more than one animal like Cat, Dog, Cow, etc. derived from it.

The Java program below demonstrates the Hierarchical Inheritance in Java.

//class Parent
class Parent
{
public void print_parent()
{
System.out.println("In ::Parent class");
}
}
//child1 class

class Child1 extends Parent
{
public void print_child1()
{
System.out.println("In ::Child1 class");
}
}
//child2 class

class Child2 extends Parent
{
public void print_child2()
{
System.out.println("In ::Child2 class");
}
}
//child3 class
class Child3 extends Parent
{
public void print_child3()
{
System.out.println("In ::Child3 class");
}
}

public class Main
{
public static void main(String[] args)
{
Child1 ch1 = new Child1(); //create a Child1 class object
ch1.print_child1(); //call its member method
Child2 ch2 = new Child2(); //create a Child2 class object
ch2.print_child2(); //call its member method
Child3 ch3 = new Child3(); //create a Child3 class object
ch3.print_child3(); //call its member method
ch3.print_parent(); //call parent class method with any object
}
}
Output:

hierarchical example output

As we see from the program we have a parent class and three child classes derived from this parent. In the main method, we create an object of each of the child classes and call their respective member methods.

For calling the methods of the parent class, note that we can use any of the child class objects as all have access to the parent class.

Multiple Inheritance In Java
Multiple inheritance is a situation in which one class can inherit from more than one class i.e. one class can have more than one parent. By doing this, the class can have more than one superclass and thus acquire the properties and behavior of all its superclasses.

The diagrammatic representation of Multiple Inheritance is shown below:

Multiple inheritance in Java

As shown above, we can derive a class from more than one class simultaneously. This way the derived class will acquire the features of all of its parent classes. This may give rise to serious ambiguities especially when the features inherited are the same.

Note: Java does not support multiple inheritance using classes. But it supports multiple inheritance using interfaces which we will discuss in our next tutorial on inheritance.

Since Java does not support multiple inheritance with classes, we will not go into the details. However, later in the tutorial, we will try to understand the reasons behind Java not supporting multiple inheritance.

Hybrid Inheritance In Java
Hybrid inheritance is a combination of one or more types of inheritances that we have discussed above. Any combination however results in a type of multiple inheritance that is not supported by Java.

A hybrid inheritance can be diagrammatically represented as below.

Hybrid Inheritance in Java

Top comments (0)