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). It is achieved by using “extends” keyword.

Inheritance represents “IS-A” relationship between super class and subclass. By inheriting we are reusing all the methods, properties and behavior of super class.

Syntax:

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 example for inheritance in java.

class Animal
{
   public void running()
   {
      System.out.println("Animal is running.");
   }
}
class Tiger extends Animal
{
   public static void main(String[] args)
   {
      Tiger obj = new Tiger();
      obj.running();
   }
}

Output:

Animal is running.

As you can see in the above example subclass Tiger can access all the attributes and behaviour of superclass Animal using “extends” keyword. This helps in reusing the code.

NOTE: private members and constructors cannot be inherited.

Advantages:

  • Code reusablity.
  • Can achieve polymorphism (method overriding) using inheritance.

Types of inheritance in java

There are five types of inheritance in java single, multilevel, hierarchical, multiple and hybrid 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.

NOTE: multiple and hybrid inheritance is not supported in java.


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 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)
   {
      Animal obj1 = new Animal();
      obj1.eat();
      Lion obj2 = new Lion();
      obj2.eat();
      obj2.roar();
   }
}

Output:

Animal is eating
Animal is eating
Lion is roaring


Multilevel inheritance

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


Also read – TreeMap in java