Inheritance in java

Let’s learn inheritance in java.

Inheritance in java

Inheritance is a procedure of acquiring all the properties and behaviour of a parent class (super class) into child class (subclass).

Inheritance represents “IS-A” relationship between super class and subclass.

Also read – abstraction in java

By inheriting we are reusing all the methods, properties and behavior of super class.

Syntax: java inheritance

class SubClass extends SuperClass
{
   // variables and methods
}

A class in java can be extended by creating new class which can retain the characteristics of the super class or parent class.

Keyword “extends” means increasing the functionality of parent class or super class.

Let’s see inheritance in java with example programs.

// super class
class Animal
{
   void eat()
   {
      System.out.println("Animal is eating.");
   }
}
// subclass
class Lion extends Animal
{
   public static void main(String[] args)
   {
      Lion obj = new Lion();
      obj.eat();
   }
}


Output:

Animal is eating.

As you can see in the above example class Lion can access all the properties and behaviour of class Animal using “extends” keyword. This helps in reusing the code.

NOTE: private members and constructors cannot be inherited.

Advantage of inheritance in java:

  • code reusablity.
  • supports runtime polymorphism.

Types of inheritance

There are five types of inheritance in java; out of which three are supported in java.

They are single inheritance, multilevel inheritance and hierarchical inheritance.

Remaining two; multiple inheritance and hybrid inheritance can be implemented through interfaces.

Single inheritance in java: in single inheritance one class inherits another class, that is, sub class inherits features of super class.

In the below example we can see single inheritance, that is, Lion class inherits features of Animal class.

import java.util.*;
class Animal
{
   void eat()
   {
      System.out.println("Animal is eating.");
   }
}
class Lion extends Animal
{
   void roar()
   {
      System.out.println("lion is roaring.");
   }
   public static void main(String[] args)
   {
      Lion obj = new Lion();
      obj.eat();
      obj.roar();
   }
}


Output:

Animal is eating.
lion is roaring.


Multilevel inheritance in java: in multilevel inheritance subclass inherits super class.

That is, in the below example Cub class inherits Lion class and Lion class inherits Animal class. Hence multilevel inheritance.

class Animal
{
   void eating()
   {
      System.out.println("animal eating");
   }
}
class Lion extends Animal
{
   void roar()
   {
      System.out.println("lion roaring");
   }
}
public class Cub extends Lion
{
   void born()
   {
      System.out.println("cub drinking milk");
   }
   public static void main(String[] args)
   {
      Animal obj1 = new Animal();
      obj1.eating();
      Lion obj2 = new Lion();
      obj2.eating();
      obj2.roar();
      Cub obj3 = new Cub();
      obj3.eating();
      obj3.roar();
      obj3.born();
   }
}


Output:

animal eating
animal eating
lion roaring
animal eating
lion roaring
cub drinking milk


Hierarchical inheritance in java: in hierarchical inheritance more than two classes inherits a single class.

Here in hierarchical inheritance two sub classes inherits a super class or parent class.

For example in the below program Dog class and Lion class inherits Animal class. Hence hierarchical inheritance.

class Animal
{
   void eating()
   {
      System.out.println("animal eating");
   }
}
class Lion extends Animal
{
   void roar()
   {
      System.out.println("lion roaring");
   }
}
public class Dog extends Animal
{
   void bark()
   {
      System.out.println("dog barking");
   }
   public static void main(String[] args)
   {
      Animal obj1 = new Animal();
      obj1.eating();
      Lion obj2 = new Lion();
      obj2.eating();
      obj2.roar();
      Dog obj3 = new Dog();
      obj3.eating();
      obj3.bark();
   }
}


Output:

animal eating
animal eating
lion roaring
animal eating
dog barking


Multiple inheritance in java

In multiple inheritance one child class or sub class can inherit features from more than one parent class or super class.

Also read – interface in java

Why multiple inheritance is not supported in java?

class X
{
   void print()
   {
   }
}
class Y
{
   void print()
   {
   }
}
class Z extends X, Y
{
   public static void main(String[] args)
   {
      Z obj = new Z();
      obj.print()
   }
}


Output:

In the above example I have class X and class Y which has method print(). Now third class Z extends both class X and Y.

Now in main() method I’m creating object ‘obj’ of class Z and I’m calling method print().

Here compiler gets confused to call method print() of class X or class Y. This type of error is called as ambiguity error.

This is the main reason multiple inheritance is not supported.

Multiple inheritance can be implemented only through interfaces.

interface M
{
   public void helloWorld();
}
interface N
{
   public void helloWorld();
}
// implementing interfaces
public class MultipleInheritanceExample implements M, N
{
   public void helloWorld()
   {
      System.out.println("helloWorld() method");
   }
   public static void main(String[] args) 
   {
      MultipleInheritanceExample obj = new MultipleInheritanceExample();
      obj.helloWorld();
   }
}


Output:

helloWorld() method

In the above example class MultipleInheritanceExample implements two interfaces: interface M and interface N.

In java a class can implement more than one interface.

By implementing interface in multiple inheritance there is no ambiguity even though we have same method helloWorld() in interface M and interface N.

Because in java, by default interface is abstract.


Hybrid inheritance in java:

is a combination of single inheritance and multiple inheritance.

Java does not support hybrid inheritance because to avoid ambiguity caused by hybrid inheritance.

To explain the ambiguity let’s take the example of diamond problem,

inheritance in java

In this (diamond) problem class D extends both class B and class C. If suppose for example there is a method in class A, say, display() method.

Now class B and class C overrides display() method in their own classes. Here comes the ambiguity.

Since class D is extending class B and class C, now if class D calls display() method, class D would be in ambiguity to use overridden method of class B or overridden method of class C.

It can be achieved through interfaces. Now let’s understand hybrid inheritance using an example using interfaces.

class A 
{
   public void display()
   {
      System.out.println("class A display method");
   }
}
interface B 
{
   public void print();
}
interface C
{
   public void print();
}
public class HybridInheritanceDemo extends A implements B, C
{
   public void print()
   {
      System.out.println("implementing print method");
   }
   public void show()
   {
      System.out.println("show method of class HybridInheritanceDemo");
   }
   public static void main(String[] args) 
   {
      HybridInheritanceDemo obj = new HybridInheritanceDemo();
      obj.show();
      obj.print();
   }
}


Output:

show method of class HybridInheritanceDemo
implementing print method