Java – Inheritance5 min read


It is one of the main features of Object-Oriented Programming (OOP). Inheritance is one of the processes or mechanisms in OOP in which one class(sub-class) acquires the properties(data members) and functionalities(methods) of another class(parent-class).

Importance of Inheritance:

  • For Code Reusability.
  • For Method Overriding (to achieve runtime polymorphism).

Important terms:

  • Super Class: The class whose properties and functionalities are inherited by the Subclass is known as superclass(a parent class or a base class).
  • Sub Class: The class that inherits the property and behavior of the other class(Superclass) is known as subclass( a derived class, extended class, or child class). The subclass can have its own fields and methods along with the fields and methods of the superclass.
  • Reusability: Inheritance supports the concept of “reusability”. This feature allows us to use the methods in the sub-class that is already present in the superclass, saving the time to rewrite the code.
  • extends: extends is the keyword that’s used by the base class so that it can inherit the properties of the superclass.


Basic Example for Inheritance:


In the above example, Breed class inherits the method ‘dogName()‘ from the superclass with the help of a keyword ‘extends’.

Note: The Object is created for only Breed class but not for Dog class. That is we can access the methods without creating the object of the superclass.
Although there are some concepts you need to know before inheriting the superclass, the method that the user wishes to inherit from the superclass cannot be private and also the superclass itself should not be declared private otherwise it cannot be inherited. ‘protected’ methods can be inherited.

Types of Inheritance:

There are three types of inheritance in java on the basis of class. They are :

  • single inheritance
  • multiple inheritance
  • hierarchical inheritance

Java does not support Multiple and Hybrid Inheritance but can be achieved by Interfaces.


1. Single Inheritance:

It refers to an inheritance where a child inherits or extends a single parent class.
Here class B extends class A.

single inheritance

2. Multilevel inheritance:

It refers to an inheritance where one class inherits from the derived class and making that derived class the superclass for the new class.
Here class C inherits from class B and class B inherits from class A.

Multiple Inheritance

3. Hierarchical Inheritance:

It refers to a relationship where more than one class can inherit or extends the same class.
Here class B and class C inherits class A.

Hierarchical Inheritance

4. Multiple Inheritance:

In this relationship, one class(derived class) can inherit from more than one superclass. Java doesn’t support multiple inheritance.
check the diagram above.

5. Hybrid Inheritance:

A combination of more than one type of inheritance in a program is called Hybrid Inheritance. It is also not supported by Java.
check the diagram above.

Constructors and super Keyword in Inheritance.

In Inheritance, a subclass inherits all the members that are its fields, methods, and nested classes from its superclass. But Constructors are not members, so they cannot be inherited by subclasses, but the constructor of the superclass can be invoked from the subclass with the help of a super keyword.
super Keyword refers directly to the superclass in its hierarchy. It is similar to this Keyword.
super is used to invoke the superclass constructor from a subclass. And also used when the members having the same name in the superclass and subclass and therefore need to differentiate between them.

Syntax of super Keyword:


Example of a super keyword to differentiate the members of the superclass from a subclass.



Java Program to check Krishnamurthy Number

In this tutorial, we will learn about Krishnamurthy numbers and write a Krishnamurthy Number program in Java. We will write two programs for Krishnamurthy number …
Read More

Java Program for ISBN Number

In this tutorial, we will learn about the ISBN (International Standard Book Number) and write a program to check for the ISBN Number in Java …
Read More

C++ Memory Management

Memory management refers to the process of managing the computer memory while assigning the space to the program’s variable to improve the overall performance. Requirement …
Read More

C++ Pointer to Pointer

As we know by now that a pointer stores the address of the pointed variable. But it is not the only use, pointer also stores …
Read More

C++ Passing Pointers to Functions

A function is a user-defined block of codes that executes some specific task assigned to it invoked by its name. If there is an argument …
Read More

C++ Null Pointer

We use a null pointer when we do not have the exact address to assign to a pointer. It is considered a good practice and …
Read More