Inheritance in java

Let’s learn inheritance in java.

Inheritance in java

Inheritance is a technique of accessing all the properties and behaviour from parent class (super class) to 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,

class Car 
{
   int price = 500000; 
}
class Baleno extends Car
{ 
   int offerPrice = 400000; 
   public static void main(String args[])
   {
      // baleno 'obj' can access field of Baleno class
      // as well as Car class
      Baleno obj = new Baleno(); 
      System.out.println("Car price : " + obj.price); 
      System.out.println("Baleno offer price : " + obj.offerPrice); 
   }
}



Output:

Car price : 500000

Baleno offer price : 400000

As you can see in the above example class Baleno object ‘obj’ can access the functionality or class members of the Car class. This helps in reusing the code.

NOTE: in inheritance we are not creating object of parent class or super class. Instead we are creating object of child class or sub class to access members of parent class or super class.


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 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.

class Animal
{
   void eating()
   {
      System.out.println("animal eating");
   }
}

class Lion extends Animal
{
   void roar()
   {
      System.out.println("lion roaring");
   }
}

public class SingleInheritanceDemo 
{
   public static void main(String[] args) 
   {
      Lion obj = new Lion();
      obj.roar();
      obj.eating();
   }
}



Output:

lion roaring
animal eating


Multilevel inheritance in java small program: in multilevel inheritance subclass will be inheriting super class and as well as sub class act as super class to other class.

That is, in the below example Cub class inherits Lion class and Lion class again 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"); 
   } 
} 

class Cub extends Lion
{ 
   void born() 
   { 
      System.out.println("cub drinking milk"); 
   } 
}

public class MultilevelInheritanceDemo 
{
   public static void main(String[] args) 
   {
      Cub obj = new Cub();
      obj.born();
      obj.roar();
      obj.eating();
   }
}



Output:

cub drinking milk
lion roaring
animal eating


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"); 
   } 
} 

class Dog extends Animal
{ 
   void bark() 
   { 
      System.out.println("dog barking"); 
   } 
}

public class HierarchicalInheritanceDemo 
{
   public static void main(String[] args) 
   {
      Dog obj = new Dog();
      obj.bark();
      obj.eating();
   }
}



Output:

dog barking
animal eating


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?

Java does not support multiple inheritance because to avoid ambiguity caused by multiple 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.

This is the main reason why java doesn’t support multiple inheritance. 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.

Like multiple inheritance, hybrid inheritance also not supported in java. 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