Inheritance In Java

The literal meaning of inheritance is acquired characteristics. You would have studied in biology that the characteristics are transferred to the children from their parents. Similarly, an object of a class acquires some properties from objects of another class.

What is Inheritance?

Inheritance is a process by using which one class acquires the properties (fields and methods) of another class.

Child Class:
Such a class that extends the properties of another class is called Child Class, Sub-Class, or Derived Class.

Parent Class:
Such a class whose properties and functionality are inherited by another class is called Parent Class, Super-Class, or Base Class.

How to Inheritance?

When a class wants to inherit another class, it has to use the extends keyword. In the same way, when an interface wants to inherit another interface, then it also has to use extends keyword, which we will learn in the upcoming post.

Example:

Class ABC extends XYZ {}

Here class ABC is extending class XYZ. So class XYZ is a parent class and class ABC is a child class.

Class Pokemon extends Pikachu {}

Here class Pokemon is extending class Pikachu. So class Pikachu is a parent class and class Pokemon is a child class.

Advantage Of Inheritance In Java

It provides us with code reusability, by which a class can use its variables and methods again and again by extending another class.

In simple words, we write basic variables and methods in a class only once and if needed, any class can use its variables and methods again and again by extending that class.

We do not have to rewrite the code which is already present in the parent class in the child class.

class ChildClass extends ParentClass
{
    // fields
    //methods
}

What are the types of Inheritance?

There are five types of inheritance. But Java does not support multiple inheritances. That’s why there are four types of inheritance in Java.

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance
  4. Hybrid Inheritance

Single Inheritance In Java

When a class is inherited by another class, then it is called Single Inheritance.

class A {
    public void print_A() { System.out.println("Class A"); }
}
 
class B extends A {
    public void print_B() { System.out.println("Class B"); }
}

Multilevel Inheritance In Java

In this Derived (child) class inherits a Base (parent), as well as the same Derived class, acts as a Base Class for some other class.

class A {
    public void print_A() { System.out.println("Class A"); }
}
 
class B extends A {
    public void print_B() { System.out.println("Class B"); }
}
 
class D extends B {
    public void print_C() { System.out.println("Class C"); }
}

Hierarchical Inheritance In Java

When two or more classes inherit the same class, it is called hierarchical inheritance.

class A {
    public void print_A() { System.out.println("Class A"); }
}
 
class B extends A {
    public void print_B() { System.out.println("Class B"); }
}
 
class C extends A {
    public void print_C() { System.out.println("Class C"); }
}

Hybrid Inheritance In Java

It is made up of two or more types of inheritance. Since Java does not support Multiple Inheritance, it also does not support Hybrid Inheritance. Hybrid Inheritance has been created by combining Hierarchical and Multiple Inheritance in the above image.

public class SolarSystem {
}
public class Earth extends SolarSystem {
}
public class Mars extends SolarSystem {
}
public class Moon extends Earth {
}

Note:

In Java, we cannot achieve Multiple Inheritance by using Inheritance. Whenever we try to do this, we get compile time error. The interface is used to achieve Multiple Inheritance, which we will know in detail in the upcoming post.

Facts About Inheritance

  1. Except for the Object class in Java, every class has some super class. Whenever a class does not have any super class, then by default Object Class is its super class.
  2. A class can have more than one sub class, but no sub class can have more than one super class.

Types Of Keyword Use In Inheritance In Java

Super Keyword In Inheritance

The super keyword in Java is a reference variable that is used to refer to immediate parent class objects.

Whenever you create the instance of a subclass, an instance of the parent class is created implicitly which is referred to by the super reference variable.

  1. super can be used to refer immediate parent class instance variable.
  2. super can be used to invoke the immediate parent class method.
  3. super() can be used to invoke an immediate parent class constructor

Final Keyword In Inheritance

The final keyword can be applied to the variables, a final variable that have no value is called a blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only. We will have detailed learning of these. Let’s first learn the basics of a final keyword.

Extend Keyword In Inheritance

The extend keyword indicates that you are making a new class that derives from an existing class. The meaning of “extends” is to increase functionality. In the terminology of Java, a class that is inherited is called a parent or superclass, and the new class is called a child or subclass.

Conclusion

Inheritance is an important pillar of OOP(Object-Oriented Programming). It is the mechanism in java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, inheritance means creating new classes based on existing ones. A class that inherits from another class can reuse the methods and fields of that class. In addition, you can add new fields and methods to your current class as well. 

Share your love