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.

Syntax:

Basic Example for Inheritance:

Output:

My Name is Bruno
German Shepard

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

Note: The Object is created for only the Breed class but not for the 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
  • multi level inheritance
  • hierarchical inheritance
Inheritance(OOP)

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

Inheritance

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

Click here to learn about each of the inheritances with examples.


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:

Numbers in super class are: 24 and: 30


Java Program for Super keyword

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

Output:

Base Class display method
Sub Class id:10
Super Class display method
Super Class id::20


MORE

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More