Inheritance in Java (Types with Example) – You Can’t Afford to Miss Out!

Inheritance means, acquiring the properties of a superclass. In general, we can say a child inherits his father’s properties and his father inherited from his father and this process goes on.  The basic principle of inheritance is passing the properties. Java Inheritance is one of the most important features of object-oriented programming, that makes it very useful.

In this article, we will discuss five types of inheritance in Java with examples. So, what are you waiting for?

So, before we start our Inheritance tutorial, I recommend you to check the concept of Classes in Java.

Stay updated with the latest technology trends while you're on the move - Join DataFlair's Telegram Channel

What is Inheritance in Java?

Inheritance in Java, allows us to carry the features of the parent class to the subclasses. Inheritance is the mechanism in which derived class acquires the properties of a base class. Here base class is the superclass and derived class is the subclass.

Some of the important terms used in Inheritance are –

  • Class: Class in Java is basically a group of objects. It is a user-defined data type or blueprint from which objects are created.
  • Super Class – A superclass in Java is the class from which the features are inherited. It is also called a parent class
  • Sub Class – A subclass in Java is the class that inherits the features of the superclass. It can have its own features and methods as well. It is a child class.
  • Reusability – The concept of reusability is supported by inheritance, the codes inside the superclass can be reused by the subclasses.

Syntax of Java Inheritance

In Java, ‘extends’ keyword is used to inherit a class. Here the BaseClass is the parent class from which the DerivedClass is acquiring the properties and DerivedClass is the derived class. The DerivedClass is inheriting the properties and methods of BaseClass.

class BaseClass
{
//methods and variables
}
class DerivedClass extends BaseClass 
{ 
   //methods and variables 
}

Inheritance in java with Example

Do you know what is Polymorphism in Java?

How Inheritance in Java Works?

Let’s see an example through which we can get an idea of how inheritance works

package com.dataflair.inheritance;
class Father
{
  String eyeColor = "Blue";
  String bloodGroup = "B+";
  String hairType = "Curls";

  void does()
  {
    System.out.println("Son Inheriting properties from Father");
  }
}
public class Son extends Father
{
  public static void main(String args[])
  {
    Father obj = new Father();
    obj.does();
    System.out.println("Blood Group of son is " + obj.bloodGroup);
    System.out.println("Eye Color of son is " + obj.eyeColor);
    System.out.println("Hair Type of son is " + obj.hairType);
    
}
}

Output-

Father-son-program-in-inheritance

Do you know how to declare and call a method in Java?

Types of Inheritance in Java

There are five types of Java Inheritance. Let’s discuss them one by one:

Inheritance in Java

1. Single Inheritance in Java

In single inheritance, the features and methods of the superclass are inherited by a single subclass.

Single Inheritance in Java

Example-

package com.dataflair.inheritance;
class Fruit
{
  void fruitname()
  {
    System.out.println("I am a Fruit");
  }
}
class Mango extends Fruit
{
  void taste()
  {
    System.out.println("My taste is sweet");
  }
}
public class SinglelevelInheritance {
  public static void main(String args[])
  {

    Mango obj = new Mango();
    obj.fruitname();
    obj.taste();

  }
}

Output-
Single-Inheritance-In-Java

Recommended Reading – Java Inner Class

b. Multilevel Inheritance in Java

In multilevel inheritance, the base class inherits the features of the superclass and simultaneously it acts as a superclass for another base class.

Here, Mango is a base class inheriting the features superclass Fruit and simultaneously it acts a superclass to Apple.

Multilevel Inheritance in Java

Example-

package com.dataflair.inheritance;
class Fruit
{
  void fruitname()
  {
    System.out.println("I am a Fruit");
  }
}
class Mango extends Fruit
{
  void taste()
  {
    System.out.println("My taste is sweet");
  }
}
class Apple extends Mango
{
  void tasteofApple()
  {
    System.out.println("My taste is sour and sweet");
  }
}
public class MultilevelInheritance {
  public static void main(String args[])
  {

    Apple obj = new Apple();
    obj.fruitname();
    obj.taste();
    obj.tasteofApple();

  }
}

Output-Multiple-Inheritance-in-java

c. Hierarchical Inheritance in Java

In hierarchical inheritance, the features of one superclass are inherited by more than one base class.

Hierarchical Inheritance in Java

Example-

package com.dataflair.inheritance;
class Fruit
{
  void fruitname()
  {
    System.out.println("I am a Fruit");
  }
}
class Mango extends Fruit
{
  void taste()
  {
    System.out.println("My taste is sweet");
  }
}
class Apple extends Fruit
{
  void tasteofApple()
  {
    System.out.println("My taste is sour and sweet");
  }
}
class Citrus extends Fruit
{
  void tasteofCitrus()
  {
    System.out.println("My taste is sour");
  }
}

public class HierarchicalInheritance {
  public static void main(String args[])
  {

    Citrus obj = new Citrus();
    Mango obj1 = new Mango();
    Apple obj2 = new Apple();
    obj.fruitname();
    obj1.taste();
    obj2.tasteofApple();
    obj.tasteofCitrus();

  }
}

Multiple-Inheritance-in-java

d. Multiple Inheritance in Java (Through Interfaces)

In Java, multiple inheritances are only possible with ‘interfaces’, this allows a base class to have more than one superclass.

It’s right to know about Interface in Java

Multiple Inheritance in Java

Example-

package com.dataflair.inheritance;
interface Fruit
{
  public void fruitname();

}
interface Mango 
{
  public void taste();

}
interface Apple extends Fruit,Mango
{
  public void tasteofApple();
}
class Citrus implements Apple
{
  @Override
  public void taste()
  {
    System.out.println("My taste is sweet");
  }
  public void tasteofCitrus()
  {
    System.out.println("My taste is sour");
  }
  public void fruitname()
  {
    System.out.println("I am a Fruit");
  }
  public void tasteofApple()
  {
    System.out.println("My taste is sour and sweet");
  }
}

public class MultipleInheritance {
  public static void main(String args[])
  {

    Citrus obj = new Citrus();

    obj.fruitname();
    obj.taste();
    obj.tasteofApple();
    obj.tasteofCitrus();

  }
}

Output-

Multiple-Inheritance-in-java

e. Hybrid Inheritance in Java (Through Interfaces)

Java Hybrid inheritance, not support by Java directly, can only via ‘interfaces’ since it is the combination of the two types mentions above.

Hybrid Inheritance in Java

Example-

package com.dataflair.inheritance;

class Fruit
{
  public void fruitname()
  {
    System.out.println("I am a Fruit");
  }

}
interface Mango
{
  public void taste();

}
interface Apple
{
  public void taste();
}
class Citrus extends Fruit implements Mango,Apple
{
  public void taste()
  {
    System.out.println("My taste is Sweet");
    System.out.println("My taste is Sweet and Sour");
    System.out.println("My taste is Sour");
  }

}
public class HybridInheritance {
  public static void main(String args[])
  {

    Citrus obj = new Citrus();

    obj.fruitname();
    obj.taste();

  }
}

Output-

Java Hybrid Inheritance with Example

Have you read – Difference between Abstraction and Encapsulation in Java

Summary

Here we concluded our Inheritance tutorial. We learned how inheritance works and its different types with examples. Hope, you liked the explanation. Please share your suggestion and feedback with us!

2 Responses

  1. Ayushi says:

    Thanks for giving these many details on inheritance in Java. I have started my java course online last week. It has got lot of syllabus. Now I have moved to inheritance topic which has objects, inheritance, and types of inheritance. Am finding some topics related to inheritance and java. I got your wonderful article about inheritance and also you have explained types of inheritance with related diagrams. Across your article, the one pictures shows what is inheritance and types of inheritance in java. Excellent summary and looking forward to share with others. Keep updating with the amazing article.

    • DataFlair Team says:

      Hi Ayushi,
      Thank you for such nice feedback on this Java tutorial. We are glad our article helps you in your learnings. We have 100+ Java tutorials, a series of Java interview questions and quizzes in our sidebar you can refer them as well. These interview questions and quizzes help with your practice.
      Keep learning and keep sharing.
      Regards,
      DataFlair

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.