Inheritance In Java

By | | Updated : 2021-12-04 | Viewed : 379 times

Inheritance In Java

Inheritance in Java is one of the main oops concepts. We should concentrate as a developer when we are using java. So here key concepts of this tutorial are what is inheritance and its types. So we will div into this tutorial to learn inheritance and apply it in Java.

What Is Inheritance

Inheritance is one of the main principles of OOPs. Inheritance means acquiring the properties like attributes and methods. For example Fruit, and Berry. Here properties of Fruit will be acquired by another class called Berry. We will see this in later sections.

In Java, the class from which properties are inherited is known as the Super/Parent class. And the class to which properties are inherited is known as Sub/Child class.

Suppose consider the example Fruit and BlueBerry classes below.

Fruit.java
public class Fruit {
    public String name = "Fruit";
    public void eat(){
        System.out.println("Fruit is an eatable item.");
    }
    
}
BlueBerry.java
public class BlueBerry extends Fruit {
    //public String name = "BlueBerry";
    public void getBlueBerryTaste() {
        System.out.println("BlueBerry's taste is sweet.");
    }
}

So here Parent class is Fruit and the Child class is BlueBerry. BlueBerry acquires the properties from the Fruit class. The properties are name field and eat() methods inherited from Fruit class to BlueBerry Child class.

MainClient.java
public class MainClient {
    public static void main(String[] args) {

        BlueBerry fruit = new BlueBerry();
        fruit.eat();
        System.out.println("Inherited Field: "+ fruit.name);
        fruit.getBlueBerryTaste();
    }
}

Once you execute the above Main method then we can see the resulted output and can perceive that properties inherited.

The result is
Fruit is an eatable item.
Inherited Field: Fruit
BlueBerry's taste is sweet.

Why Do We Use Inheritance

Inheritance always deals with inheriting the properties which means code reusability. Common properties of Parent and Child can not be implemented in both classes as these properties will be inherited from Parent into Child class.

Types Of Inheritance In Java

Different types of inheritance in Java

  • Single Inheritance

  • Multiple Inheritance

  • Multi-Level Inheritance

  • Hierarchical Inheritance

  • Hybrid Inheritance

Single Inheritance

In Single inheritance, only one Child class will be used for inheritance properties Parent Class. So all the properties of the Parent class will be inherited to that particular Child Class.

Consider Fruit and BlueBerry classes. See below code snippets.

Fruit.java
public class Fruit {
    public String name = "Fruit";
    public void eat(){
        System.out.println("Fruit is an eatable item.");
    }
    
}
BlueBerry.java
public class BlueBerry extends Fruit {
    //public String name = "BlueBerry";
    public void getBlueBerryTaste() {
        System.out.println("BlueBerry's taste is sweet.");
    }
}

When the BlueBerry object is instantiated then all the properties that name and eat() are inherited. Here only one Child class is involved in this relationship.

Multiple Inheritance

In Multiple Inheritance, one Subclass should inherit from multiple classes. Using classes Multiple inheritance in Java is not possible as Java does not support them. We must use interfaces. Notice below given example for Multiple inheritance.

We write code for Multiple inheritance. Here we use Berry, Fruit interfaces. And These two interfaces have only Child Class which is BlueBerry. Notice below classes.

Berry.java
public interface Berry {
    public void getFruitCategory();
}
Fruit.java
public interface Fruit {
    public void eat();
}
BlueBerry.java
public class BlueBerry implements Fruit, Berry {

    public void getBlueBerryTaste() {
        System.out.println("BlueBerry's taste is sweet.");
    }

    @Override
    public void getFruitCategory() {
        System.out.println("The fruit category is Berry");
    }

    @Override
    public void eat() {
        System.out.println("Fruit is an eatable item.");
    }
}

You can see here multiple Parent properties is been inherited into Child classes. As Java does not support extends keyword for extending more than one class, we used here interfaces.

Multi-Level Inheritance

In Multi-level inheritance, The Child class which is inheriting from the parent class is also Parent Class to Other Child Class. So one of the classes will become Child to its parent and at the same time, it will become a Parent class to its Child class.

Now we write code for multi-level inheritance.

Fruit.java
public class Fruit {
    public void eat() {
        System.out.println("Fruit is an eatable item.");
    }

}
Berry.java
public class Berry extends Fruit {
    public void getFruitCategory() {
        System.out.println("The fruit category is Berry");
    }
}
BlueBerry.java
public class BlueBerry extends Fruit {
    public void getBlueBerryTaste() {
        System.out.println("BlueBerry's taste is sweet.");
    }
}

Notice here Berry class is Child class to Fruit at same it is Parent to BlueBerry class. Now all the parent's properties will be inherited into the BlueBerry class.

Hierarchical Inheritance

In Hierarchical inheritance, multiple Child classes should be inherited from the same Parent class. Means one parent class should have two or more Child classes. We will understand once we write one example.

Fruit.java
public class Fruit {
    public void eat() {
        System.out.println("Fruit is an eatable item.");
    }

}
Apple.java
public class Apple extends Fruit {
    public void getAppleTaste() {
        System.out.println("Apple's taste is sweet.");
    }
}
BlueBerry.java
public class BlueBerry extends Fruit {
    public void getBlueBerryTaste() {
        System.out.println("BlueBerry's taste is sweet.");
    }
}

Hybrid Inheritance

Hybrid inheritance is a combination of two or more. One of the examples is Hybrid is two classes are inherited from one parent and these two classes again will become Parent classes to another Child class. We will get a clear idea if we have a nice example as below.

Note: It is not possible to use classes in Hybrid inheritance. So we will use here interfaces.

Berry.java
public interface Berry {
    void getFruitCategory();
}
BlackBerry.java
public interface BlackBerry extends Berry {
    void getBlackBerryTaste();
}
BlueBerry.java
public interface BlueBerry extends Berry {
    void getBlueBerryTaste();
}
HybridBerry.java
public class HybridBerry implements BlackBerry, BlueBerry {

    @Override
    public void getFruitCategory() {
        System.out.println("The fruit category is Berry");
    }

    @Override
    public void getBlueBerryTaste() {
        System.out.println("BlueBerry's taste is sweet.");
    }

    @Override
    public void getBlackBerryTaste() {
        System.out.println("BlackBerry's taste is sweet.");
    }

}

See here HybridBerry implemented both Berry Parent classes. So here inherited properties are getFruitCategory(), getBlueBerryTaste() and getBlackBerryTaste().

For code repo for Inheritance In Java, you can get the entire code Java-Inheritance-Example-App

Leave A Reply