Inheritance in Java

Inheritance in Java

Inheritance is inheriting the properties of the parent class into the child class. It is a mechanism by which one object acquires all the properties and behaviors of its parent. Inheritance represents the "IS-A" relationship, which is also known as the parent-child relationship.

Inheritance is achieved using the "extends" keyword.

There can be only one super class because java doesn't support multiple inheritance.

class Animal
{
    void eat()
    {
        System.out.println("I am eating");
    }
}
class Dog extends Animal
{
    public static void main(String[] args)
    {
        Dog d=new Dog();
        d.eat();
    }
}

Types of Inheritance

  1. Single-level inheritance

  2. Multi-level Inheritance

  3. Hierarchical Inheritance

  4. Multiple Inheritance

  5. Hybrid Inheritance

1. Single-level inheritance

In single-level inheritance, only one class is derived from the parent class. In the below representation, Class B inherits the properties of Class A.

class Animal
{
    void eat()
    {
        System.out.println("I am eating");
    }
}
class Dog extends Animal
{
    public static void main(String[] args)
    {
        Dog d=new Dog();
        d.eat();
    }
}

2. Multi-level Inheritance

The multi-level inheritance includes the involvement of at least two or more than two classes. One class inherits the features from a parent class, and the newly created sub-class becomes the base class for another new class.

class A
{
    void showA()
    {
        System.out.println("a class method");
    }
}
class B extends A
{
    void showB()
    {
        System.out.println("b class method");
    }
}
class C extends B
{
    void showC()
    {
        System.out.println("c class method");
    }
    public static void main(String[] args)
    {
        A obj1=new A();
        obj1.showA();

        B obj2=new B();
        obj2.showA();
        obj2.showB();

        C obj3=new C();
        obj3.showA();
        obj3.showB();
        obj3.showC();
    }
}

3. Hierarchical Inheritance

The type of inheritance where many subclasses inherit from one single class is known as "hierarchical inheritance."

class A
{
    void showA()
    {
        System.out.println("a class method");
    }
}
class B extends A
{
    void showB()
    {
        System.out.println("b class method");
    }
}
class C extends A
{
    void showC()
    {
        System.out.println("c class method");
    }
    public static void main(String[] args)
    {
        A obj1=new A();
        obj1.showA();

        B obj2=new B();
        obj2.showA();
        obj2.showB();

        C obj3=new C();
        obj3.showA();
        obj3.showC();
    }
}

4. Multiple Inheritance

Multiple inheritance is a type of inheritance where a subclass can inherit features from more than one parent class. Multiple inheritance is not available in Java.

5. Hybrid Inheritance

Hybrid inheritance is a combination of more than two types of inheritance single and multiple. It is basically the combination of simple, multiple, hierarchical inheritances. Hybrid inheritance is not supported by Java.

Advantages

  • code reusability

  • promotes run time polymorphism by allowing method overriding

Disadvantages

  • Using inheritance the two classes parent and child get tightly coupled