Wednesday , January 18 2017
Home / java / OOPS / Types Of Inheritance in Java

Types Of Inheritance in Java

Inheritance is the key feature for the Object Oriented Programming as there is class to inherit the property allocated. Inheritance in Java occurs when the single class extends with another class then it inherits the non-private members. This includes the fields and methods that is made in the process.

The Inheritance in Java can be determined using examples like the Parent and Child relationship so that the Super class can be called as the Parent and the Sub class can be called as the child. The Inheritance can be defined as the relationship between Super class and Sub class. The extends and implement keywords can be useful for describing the inheritance in Java. In short the Inheritance is defined as the relationship between the Super class with the Sub class and also implementing the Java language.

Single Inheritancesingle Inheritance

When the class gets extends to another class then it can be useful for calling them as single inheritance. For example, when class B extends another class so that they can be called as single inheritance. A is a parent class and B be a child 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());
    }
}

Output:

B : B’s Method

Multiple Inheritance

Multiple Inheritance In Multiple Inheritance one class can be extending from more than one base classes. Multiple Inheritance is also one of the rarely used oops but there are many classes included in the concepts. The unwanted complexity results will have the further extending according to the class. Aggregation that is restricted in the class are called as the composition. There are also many new languages like Java, Small Talk, C# and many others that do not get support the Multiple inheritance. The C++ supports the Multiple Inheritance accurately for the OOPs concepts in the Java.

Multilevel InheritanceMultilevel Inheritance

Multilevel inheritance is the mechanism to inherit the derived class with the base class to new class. For example the subclass C is the subclass of B can be derived and B is a class of A. Association is the abstraction in relationship between two objects. The multiple inheritance is the derived class that manages dependency of the two base classes. The class C inheriting method and properties of class A with the B then it can also be called as the multilevel inheritance OOPs concepts in the Java.

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());
    }
}

Output:

A: B’s Method

Hierarchical InheritanceHierarchical Inheritance

One class inheritance are inherited through the sub classes. The object that has another type of object in the same can be called as the Composition and they are combined with the existence of object that is contained in the same place. For example the class B, C inherits same class A and the class A is parent class that can be called as the base class. This type of Hierarchical Inheritance present in the Java is held.

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());
    }
}

Output:

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

Hybrid Inheritance

The Hybrid inheritance is the combination of both the Single and Multiple inheritances. The hybrid inheritance is achieved with the interfaces in Java. There are also multiple class methods for the parent class according to the sub class.

Hybrid Inheritance

 

 

About chandrashekhar

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+ ,

Check Also

Difference between Aggregation and Composition

Aggregation: Aggregation is one of the special case of Association. The directional association present in …

Leave a Reply

Your email address will not be published. Required fields are marked *