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

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

Advantages:

  • code reusablity.
  • supports runtime polymorphism.

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