Types Of Inheritance in Java

By | 2017-12-15T22:36:14+00:00 May 23rd, 2015|OOPS|1 Comment

Inheritance in Java :

Inheritance is a key feature of Object Oriented Programming. Deriving a new class from existing class called inheritance.

Here the main advantage of deriving a class from existing class is : The newly created (derived) class acquires all the features of exiting class from where it is derived.

Inheritance makes a relationship between the two classes : Parent and Child.

The Inheritance relationship means, that the sub class inherits the members of the super class. Here the members means : instance variables and methods.

For Example : Benz is a subclass of Car, that means Benz automatically inherits the instance variables and methods of Car class.

In Java we can implement the inheritance by using extends keyword.

Types Of Inheritance :

Here are the different types of inheritance in Java. Lets see each and every type with neat example.

Single Inheritance :

Single inheritance is a simple and easy to understand. A class is created by extending an another class is called Single inheritance.

single InheritanceOn the above diagram Class B is extended from Class A, then this type of deriving is called single inheritance. Here class A is a parent class of class B. In other words, Class B is a child of Class A.

Exmple:

class A {
    public String meth() {
        return "A's Method";
    }
}
class B extends A {
    public String meth() {
        return "B's Method";
    }
    public String meth2() {
        return "B's Method2";
    }
}
public class SingleInheritanceDemo {
    public static void main(String[] args) {
        B b = new B();
        System.out.println("B : " + b.meth());
    }
}

[box type=”success” align=”alignleft” class=”” width=”100%”]

B : B’s Method

[/box]

Multiple Inheritance :

Multiple Inheritance means A class created by deriving more than one super class is called as Multiple Inheritance.

Multiple Inheritance

On the above diagram refers multiple inheritance: Here Class C is derived from Class A and Class B.

This is one of rarely used inheritance type in software development. Because it leads some unwanted complexity when further extending the classes. Because of this reason, some of the languages like Java, C# and Smalltalk doesn’t support the Multiple inheritance.

Multilevel Inheritance :

Multilevel Inheritance refers to when a class extends from another class which extends another class is called Multilevel inheritance.

Multilevel Inheritance

On the above diagram represents the multilevel inheritance, here Class C extended from Class B and Class B extended from the Class A,

Example:

class A {
    public String meth() {
        return "A's Method";
    }
}
class B extends A {
    public String meth() {
        return "B's Method";
    }
}
class C extends B {
    public String meth1() {
        return "C's Method";
    }
}
public class MultiLevelInheritanceDemo {
    public static void main(String[] args) {
        A a = new C();
        System.out.println("A: " + a.meth());
    }
}

[box type=”success” align=”alignleft” class=”” width=”100%”]

A: B’s Method

[/box]

Hierarchical Inheritance :

When multiple classes are inherited from a single class is called Hierarchical inheritance.

Hierarchical Inheritance

On the above diagram Class B and Class C are inherited from a single class A, this represents the Hierarchical inheritance.

Example:

class A {
    public String meth() {
        return "A's Method";
    }
}
class B extends A {
    public String meth() {
        return "B's Method";
    }
}
class C extends A {
    public String meth2() {
        return "C's Method";
    }
}
public class HierarchicallInheritanceDemo {
    public static void main(String[] args) {
        A a = new C();
        System.out.println("C's A : " + a.meth());
        B b = new B();
        System.out.println("B's B : " + b.meth());
    }
}

[box type=”success” align=”alignleft” class=”” width=”100%”]

C’s A : A’s Method
B’s b : B’s Method

[/box]

Hybrid Inheritance :

The Hybrid inheritance is the combination of both the Single and Multiple inheritances.

Hybrid Inheritance

The hybrid inheritance is achieved with the interfaces in Java.

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

One Comment

  1. arorapriya965@gmail.com'
    Priya December 15, 2017 at 5:23 am - Reply

    Inheritance promotes reusability. When a class inherits or derives another class, it can access all the functionality of inherited class. Thanks for sharing this , keep posting.

Leave A Comment