Java Inheritance With Examples

What is Inheritance?

Inheritance in Java is a methodology by which a class can inherit/acquire the state and behavior of any other class.  By this, we can reuse the features which are already available. After inheriting, a class can add its own features as well.

The concept has its origin from our biological inheritance. We inherit some features from our parents, and apart from it, we develop some distinct features of self.

There are several conventions. The class which inherits is called the subclass, derived class, or child class, whereas the class which is inherited is called the superclass, base class, or parent class. We would be using the parent-child convention throughout.

Parent and Child class share IS-A relationship.

General Format of Inheritance

Class NewClass extends ExistingClassToInherit{
//Newly defined Methods and Fields
}

Java has a keyword extends, for the purpose of inheriting. It indicates that the NewClass(child class) to be created, will be acquiring features of ExistingClassToInherit(parent class).

Example:

Say, we have an existing class Parent, which has 3 attributes: name, age, and gender.

class Parent{
   String name;
   int age;
   String gender;
}

We want to create a class Child, that has 4 attributes: name, age, gender,  and myNewFeature. So instead of writing all of it again, we can simply inherit class Parent. It will acquire the existing attributes of parent. So the only attribute we need to work for is myNewFeature.

class Child extends Parent{
    String myNewFeature;
     void myDisplay(){
     System.out.println("Name:"+name+" ,Age:"+age+" ,Gender:"+gender+" ,myNewFeature:"+myNewFeature);
    }   
}

Let’s add main, to see the impact of extends.

class Parent{
   String name;
   int age;
   String gender;
}

class Child extends Parent{
    String myNewFeature;
    void myDisplay(){
        System.out.println("Name:"+name+" ,Age:"+age+" ,Gender:"+gender+" ,myNewFeature:"+myNewFeature);
    }
}

public class ClassWithMain{
    public static void main(String args[]){
        Child childObj = new Child();
        childObj.name="Muttu";
        childObj.age=18;
        childObj.gender="M";
        childObj.myNewFeature="Flying";

        childObj.myDisplay();
 }
}

Output:

Name:Muttu ,Age:18 ,Gender:M ,myNewFeature:Flying

Types of Inheritance

Java supports three types of Inheritance of class.

  • Single
  • Multilevel
  • Hierarchical

The interface is also inherited,  and it will be discussed in the upcoming lectures.

Single Level Inheritance

The simplest form of Inheritance where a class inherits another class as we did in the earlier example. We have class Level1 (member method: level1Method). We create class Level2 extending Level1(member method: level2Method, inherited method: level1Method).

Example:

class Level1{
  void level1Method(){
      System.out.println("I am Level1 method");
  }
}

class Level2 extends Level1{
   void level2Method(){
      System.out.println("I am Level2 method");
  }
}

public class ClassWithMain{
    public static void main(String args[]){
        Level2 childObj = new Level2();

        childObj.level1Method();
        childObj.level2Method();
    }
}

Output:

I am Level1 method
I am Level2 method

 

Multi-Level Inheritance

A chain of single inheritance is called multi-level inheritance. Extending our previous example, We now create Level3 which will inherit class Level2, which is inheriting class Level1. As a result, we have the following member list

Level1: member method is level1Method.

Level2 extends Level1: member method is level2Method, the inherited method is level1Method.

Level3 extends Level2: member method is level3Method, the inherited method is level1Method and level2Method.

Example:

class Level1{
  void level1Method(){
      System.out.println("I am Level1 method");
  }
}

class Level2 extends Level1{
   void level2Method(){
      System.out.println("I am Level2 method");
  }
}

class Level3 extends Level2{
   void level3Method(){
      System.out.println("I am Level3 method");
  }
}


public class ClassWithMain{
    public static void main(String args[]){
        Level3 childObj = new Level3();
        childObj.level1Method();
        childObj.level2Method();
        childObj.level3Method();
    }
}

Output:

I am Level1 method
I am Level2 method
I am Level3 method

Hierarchical Inheritance

The scenario where 2 or more classes inherit a single class, it is known as hierarchical inheritance. We will create 2 classes, Level2A and Level2B. Both will be inheriting from the same parent class Level1. As a result, we will have the following member list.

Level1: member method is level1Method.

Level2A extends Level1: member method is level2AMethod, the inherited method is level1Method.

Level2B extends Level1: member method is level2BMethod, the inherited method is level1Method.

Example:

class Level1{
  void level1Method(){
      System.out.println("I am Level1 method");
  }
}

class Level2A extends Level1{
   void level2AMethod(){
      System.out.println("I am Level2A method");
  }
}

class Level2B extends Level1{
   void level2BMethod(){
      System.out.println("I am Level2B method");
  }
}


public class ClassWithMain{
    public static void main(String args[]){
        Level2A childObj2A = new Level2A();
        Level2B childObj2B = new Level2B();

        System.out.println("Level2A Object");
        childObj2A.level1Method();
        childObj2A.level2AMethod();

        System.out.println("\nLevel2B Object");   
        childObj2B.level1Method();
        childObj2B.level2BMethod();
    }
}

Output:

Level2A Object
I am Level1 method
I am Level2A method

Level2B Object
I am Level1 method
I am Level2B method

There is another type of inheritance, called multiple inheritance. When a class inherits from multiple classes, it is called multiple inheritance. But this inheritance is not supported by Java.

Let us understand the situation as to why this inheritance is not supported.

We have 2 classes, Level2A and Level2B. Both inherited from the same parent class Level1. As a result, we will have the following member list.

Level1: member method is level1Method.

Level2A extends Level1: member method is level2AMethod, the inherited method is level1Method.

Level2B extends Level1: member method is level2BMethod, the inherited method is level1Method.

Now we create, class Level3 which inherits Level2A and Level2B class. 

Level3 extends Level2A, Level2B: member method is level3Method, the inherited method is level2AMethod, level2BMethod, and level1Method. This level1Method is the root of ambiguity. It is to be considered a member of which class? Level2A or Level2B?

Hence, in order to remove any such ambiguity, Java restricts the inheritance to only single, multilevel, and hierarchical.

This was all about Inheritance in Java. We will discuss the Overloading concept in the next lecture.