What is Inheritance in Java with Examples

Inheritance is considered as one of the core features of any Object-Oriented Programming Language.
In this post, I will be sharing what is Inheritance in java, real-life example of Inheritance, general syntax of Inheritance in java, types of Inheritance in java, and rules of Inheritance in java. Let's dive deep into it.

Read Also:  Polymorphism in Java

Introduction to Inheritance in Java

The meaning of the word "Inheritance" in the field of programming is similar to that of in the field of biology.

Inheritance is the feature or concept by which one class can acquire or inherit the properties and methods of another class. We can categorize the classes involved in Inheritance into two types:

1. Parent Class: The class whose properties and methods are acquired by another class is called Parent Class. It is also called as Super Class or Base Class.

2. Child Class: The class which acquires the features of the parent class is called as Child Class. It is also called Sub Class or Derived Class respectively.

Inheritance in Java


This concept of inheritance makes the code more reusable.

General Syntax for Inheritance in Java

In Java, Child Class can inherit the Parent Class using extends keyword. The general syntax for inheritance in java is shown below:

class ParentClass
{

  property1, property2, property3....;

   method1()
  {
    //body
  }

   method2()
  {
    //body
  }
}


class ChildClass extends ParentClass
{
   //body
}


Real-Life Example of Inheritance

In real life, we usually see that a child or offspring acquire the properties of his / her parents. The current generation inherits the previous generation as few of their properties matches with each other. This is called Inheritance in real life.

In the field of computer programming, the concept of Inheritance has a similar meaning.

In Java, we can use properties and features of parent class and can extend it in the form of child class which has all the properties and methods of its parent class along with some new features.

One can also change a few properties or methods acquired from the parent class in the child class with the help of Polymorphism in Java.

Types of Inheritance in Java

Upon the basis of implementation, Inheritance can be classified into four types:

1. Single Inheritance: In the case of Single Inheritance, only one child is derived from the parent class. The level of inheritance, in this case, is only one which means that only one child class inherits the parent class.

single inheritance in java


Let’s have a look over an example of Single Inheritance.

class A
{
    int i = 10;
}


class B extends A //inheriting parent class

{
    void display()
    {
        System.out.println(i);
    }
}


public class JavaHungry{

    public static void main(String a[])
    {
        B obj = new B();
        obj.display();
    }
}

Output:
10

In the above example class B extends class A which means class B is child class which is derived from parent class A. Child class B has the same member variable ‘i’ as class A and has one new method display() which is not present in the parent class A.


2. Multi-Level Inheritance: In Multi-Level Inheritance, as the name suggests, multiple levels of inheritance is observed. It means that one child class is derived from one parent class. Now, this child class act as a parent class and another child class is derived from it. This process goes on.

Multi level Inheritance in Java


Let’s have a look over Multi-Level Inheritance with the help of below example code:

class A
{
    int a = 50;
}

class B extends A  // B is child of A
{
    int b = a*a;
}

class C extends B // C is grandchild of A
{
    void display()
    {
        System.out.println(a);
        System.out.println(b);
    }
}


public class JavaHungry
{
    public static void main(String args[])
    {
        C obj = new C();
        obj.display();
    }
}

Output:
50
2500


In the above example, class B is derived from class A and class C is derived from class B. In this example, the total level of inheritance is two. In general, multi-level inheritance may consist of 2 or more levels.

3. Multiple Inheritance: Java doesn’t support Multiple Inheritance which means that a class can’t inherit more than one class.



Multiple Inheritance in Java


The above scenario shown in the figure is not allowed in Java. However one can use interfaces in order to explicitly implement multiple inheritance in java.

4. Hierarchical Inheritance: In hierarchical Inheritance, more than one child class can be derived from the base class. All the child classes can inherit similar properties from the parent class.


Hierarchical Inheritance in Java

Let’s have a look over below example:

class Num
{
    int a,b,c;
    Num(int num1,int num2)
    {
        a = num1;
        b = num2;
    }

    Num(int num1,int num2,int num3)
    {
        a = num1;
        b = num2;
        c = num3;
    }
}

class Add extends Num
{
    int result;
    Add(int a, int b, int c)
    {
        super(a,b,c);
        result= a+b+c;
    }   

    void display()
    {
        System.out.println(result);
    }
}

class Mult extends Num
{
    int result;

    Mult(int a, int b)
    {
        super(a,b);
        result= a*b;
    }

    void display()
    {
        System.out.println(result);
    }
}

public class JavaHungry
{
    public static void main(String a[])
    {
        Add obj1 = new Add(1,2,3);
        obj1.display();

        Mult obj2 = new Mult(11,23);
        obj2.display();
    }
}

Output:
6
253


In the above example, Class Num is acting as a parent class whereas Class Add and Class Mult are child class of it. The method super() is called inside constructors of child classes. With the help of this super() we can call the constructor of the parent class. Along with it, this keyword is used to refer to variables or methods of the current class.

5. Hybrid Inheritance:  It is the combination of Single and Multiple inheritance in java. Java does not support the concept of Hybrid Inheritance. In Java, only through the interface, we can achieve Hybrid Inheritance. For example, you can see below Class A is a parent class of Class B and Class C whereas Class B and Class C are parent class for Class D.


hybrid inheritance in java


Rules of Inheritance in Java


 1. Private members of a class can’t be inherited by another class. For example:

class A
{
    int a;
    private int b;
    A()
    {
        a = 10;
        b = 25;
    }
}

class B extends A
{
    void display()
    {
        System.out.println(a+" "+b);
    }
}

public class JavaHungry
{
    public static void main(String args[])
    {
        B obj = new B();
        obj.display();
    }
}

Output:

/JavaHungry.java:16: error: b has private access in A
        System.out.println(a+" "+b);
                                 ^
1 error


When we compile the code shown above, it’ll throw a compiler error like above because private members (in this case, ‘b’ is a private member ) of parent class can’t be inherited by the child class.

2. Whenever there is confusion between members of the current class and parent class, in such case we can use this keyword to refer to the members of the current class and super keyword to refer to the members of the parent class. These keywords can solve the ambiguity created during the method overriding or conflict caused because of the change in the value of a variable in the base class. For example:

class A
{
    int a = 10;
    int b = 20;
    public void display()
    {
        System.out.println("In Parent Class: a = "+a+" and b = "+b);
    }
}

class B extends A
{
    int a = 30;
    int b = 22;
    public void display()
    {
        System.out.println("In Child Class: a = "+this.a+" and b = "+this.b);
        super.display();
    }
}

public class JavaHungry{
    public static void main(String args[])
    {
        B obj = new B();
        obj.display();
    }
}

Output:
In Child Class: a = 30 and b = 22
In Parent Class: a = 10 and b = 20


Here display() method of child class is called inside main(). The display() of child class firstly prints it’s local members a and b using this keyword and then calls the display() method of parent class using super keyword.

3.  The constructor of the parent class can be called using the super() method inside the constructor of the child class. The super() method must be the first line inside the constructor of child class in order to call parent class’s constructor otherwise it may generate an error.


class A
{
    int a1,a2;
    A(int x, int y)
    {
        a1 = x;
        a2 = y;
    }
    void display1()
    {
        System.out.println(a1+" "+a2);
    }
}

class B extends A
{
    int b1,b2;
    B(int p, int q, int r, int s)
    {
        super(p,q);
        b1 = r;
        b2 = s;
    }
    void display2()
    {
        System.out.println(a1+" "+a2+" "+b1+" "+b2);
    }
}

public class JavaHungry
{
    public static void main(String args[])
    {
        B obj = new B(10,20,30,40);
        obj.display1();
        obj.display2();

    }
}

Output:

10 20
10 20 30 40

That's all for today, please mention in comments if you have any questions related to what is inheritance in java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry