exampracticeonline.com
Inheritance

Inheritance in Java

Inheritance in java is one pillar of OOP(Object Oriented Programming). It is the mechanism in java by which one class can inherit the features(fields and methods) of parent class.

Inheritance is used when we have is-a relationship between objects. Inheritance in Java is implemented using extends keyword.

Inheritance is a mechanism in which one class(child class) acquires the property of another class(parent class). 

We can reuse the fields and methods of the existing class. Hence, inheritance facilitates Reusability and is an important concept of OOPs.

When use inheritance in java

  • In Java, when an "Is-A" relationship exists between two classes we use Inheritance.
  • When we want to use same functionality of Class in new derived Class. We can use functionality of other Class by inheritance.
  • When we want to override functionality of Class in new derived Class.

Why use inheritance in java

  • Reusability: Inheritance supports the concept of “code reusability”, i.e. If we want to create a new class and we have already a class that includes same functionality that we want, we can inherit code in our new class from the existing class. By doing this, we can reuse the fields and methods of the existing class.

Important terms in Inheritance

  • Class
  • Sub Class/Child Class
  • Super Class/Parent Class
  • extends keyword
  • super keyword

Class

class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type.

Classes and Objects are basic concepts of Object Oriented Programming which revolve around the real life entities.

Read Class and Object in details

Super Class/Parent Class

Super Class is a parent class that contains functionality to be inherited and these functionality inherit by child Class using extends keywords. Super Class is also called parent Class.

Syntax:

// Super Class

class Parent {                   

      // variables and methods

// Child Class

class Child extends Parent {

     // inherit variables and methods

}

Sub Class/Child Class/Derived Class

Sub Class is a child class that is inheriting the properties of Parent Class by using extends keywords. Sub Class is also called child Class or derived Class.

Syntax:

// Super Class

class Parent {                   

      // variables and methods

// Child Class

class Child extends Parent {

     // inherit variables and methods

}

extends Keyword

extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword.

Syntax:

// Super Class

class Parent {                   

      // variables and methods

// Child Class

class Child extends Parent {

     // inherit variables and methods

}

super keyword

The super keyword is similar to this keyword. Following are the scenarios where the super keyword is used.

  • It is used to differentiate the members of superclass from the members of subclass, if they have same names.

  • It is used to invoke the superclass constructor from subclass.

Syntax: 

// Super Class

class Parent {                   

      // variables and methods

      int num = 20;

      // display method of superclass

      public void display() {

            System.out.println("display method of superclass");

       }

// Child Class

class Child extends Parent {

     // inherit variables and methods

     int num = 30;

   // display method of sub class
   public void display() {
      System.out.println("display method of subclass");
   }

   public void show() { 

       Child ch = new Child(); 
       // Calling parent class method 
       ch.display();

       // Calling parent class method 
       super.display();              
 
      // printing the value of variable num of subclass
      System.out.println("value of the variable num of sub class: "+ ch.num);

      // printing the value of variable num of superclass
      System.out.println("value of the variable num of super class: "+ super.num);

   }
   public static void main(String args[]) {
      Child obj = new Child();
      obj.show();
   }

}


Output: 

display method of subclass

display method of superclass

value of the variable num of sub class: 30

value of the variable num of super class: 20

Types of Inheritance in Java

Below are the different types of inheritance supported by Java.

  1. Single Inheritance 
  2. Multilevel Inheritance
  3. Hierarchical Inheritance
  4. Multiple Inheriatance (Through Interfaces) 
  5. Hybrid Inheritance(Through Interfaces)