Inheritance in java

Today we are going to learn inheritance in java.

Inheritance in java

Inheritance is a technique of accessing all the properties and behavior from one class (super class) to another class (subclass). It 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:

class SubClass extends SuperClass
{
   // code
}

A class in java can be extended by creating new classes which can retain the characteristics of the base class.

Keyword “extends” means increasing the functionality of superclass. Let’s see java inheritance example,

class Car 
{
   int price = 500000; 
} 
class Baleno extends Car
{ 
   int offerPrice = 10000; 
   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 : 10000


Types of inheritance

There are five types of inheritance out of which three are supported in java. They are single, multilevel and hierarchical inheritance.

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

Single inheritance: here subclass inherits features of one superclass.

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: here subclass will be inheriting superclass and as well as subclass act as superclass to other class.

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: here one class serves as superclass for more than one subclass.

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.eating();
      Lion obj1 = new Lion();
      obj1.roar();
      obj.eating();
   }
}

Output:

animal eating
lion roaring
animal eating


Multiple inheritance: Multiple inheritance can be achieved only through interfaces.

Also read – interface in java

Here one class can have more than one superclass.

interface A 
{ 
   public void displayHello(); 
} 

interface B 
{ 
   public void displayWorld();
}

interface C extends A, B
{ 
   public void displayHello();
}

class SubClass implements C
{
   @Override
   public void displayHello()
   {
      System.out.println("Hello");
   }

   public void displayWorld()
   {
      System.out.println("World");
   }
}

class MultipleInheritanceDemo 
{
   public static void main(String[] args) 
   {
      SubClass obj = new SubClass();
      obj.displayHello();
      obj.displayWorld();
      obj.displayHello();
   }
}

Output:

Hello
World
Hello


Hybrid inheritance: is a combination of hierarchical and multiple inheritance. Hybrid inheritance is not supported in java. It can be achieved through interfaces.

inheritance in java