Inheritance in java

Let’s learn what is the 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. 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.

// super class
class Animal
{
   // attributes and behaviour of super class
   String name;
   public void eat()
   {
      System.out.println("Animal is eating.");
   }
}
// subclass - inheriting from Animal class
class Lion extends Animal
{
   // new method in subclass
   public void print()
   {
      System.out.println("Lion name is " + name);
   }
}
class Main
{
   public static void main(String[] args)
   {
      Lion obj = new Lion();
      obj.name = "Royal Asiatic Lion";
      obj.print();
      // calling method of superclass using object of subclass
      obj.eat();
   }
}

Output:

Lion name is Royal Asiatic Lion
Animal is eating.

As you can see in the above example subclass Lion 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:


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: Which inheritance in java programming is not supported? multiple and hybrid inheritance.


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.

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