Java Inheritance

It is one of the main features of Object-Oriented Programming (OOP). Inheritance is one of the process 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.

Syntax:

Simple Example for Inheritance:

Output:

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

Note: that 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
Inheritance in Java

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

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

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

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

Java 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:

Output:


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

Output: