Programs

Types of Inheritance in Java: Single, Multiple, Multilevel & Hybrid

Introduction

The programming language “Java” is widely used in the development of applications for mobile, web, desktop, etc. It is a robust, high-level, and object-oriented programming language developed by Sun Microsystems in 1995. Despite being a programming language, Java has an API and a runtime environment (JRE) and therefore, also called as a platform.

Several concepts are there in Java, with four main concepts to get hold of the language. They are abstraction, encapsulation, inheritance, and polymorphism. 

In this article, we will be focusing on the concept of inheritance in Java and the types of inheritance in java.

What is Inheritance?

As the name suggests, inheritance means inheriting the properties.  The mechanism by which a class is allowed to derive the properties of a different class is termed inheritance.  With the concept of inheritance, the information in a program can be organized hierarchically.

In Java, the concept of inheritance allows the users to create classes and use the properties of existing classes. 

A few important terminologies associated with the concept are:

  • Class: It is defined as a group of objects sharing common properties.
  • Sub Class: Also known as a derived or extended class, this class inherits the features from another class. Also along with the inherited fields and methods, the class generated from the class can add fields and methods of its own.
  • Super Class: The superclass represents the class whose features are inherited by a sub-class.
  • Reusability: The technique of reusability allows the user to create a class (a new one) having the fields or methods of an already existing class. It allows reusing the code.

Importance of Java inheritance

Implementation of inheritance in Java provides the following benefits:

  • Inheritance minimizes the complexity of a code by minimizing duplicate code. If the same code has to be used by another class, it can simply be inherited from that class to its sub-class. Hence, the code is better organized.
  • The efficiency of execution of a code increases as the code is organized in a simpler form.
  • The concept of polymorphism can be used along with inheritance. 

Syntax

The basic syntax is

Class superclass {

—————- 

}

class subclass extends superclass  

{  

————–

—————– 

}  

The keyword “extends” is used while inheriting a class. It defines that the functionality of the superclass is being extended to the subclass.

The new class created is called a sub-class while the inherited class is termed a parent class.

Figure 1: An example of a code applying java inheritance is shown below

Source

Two classes Calculation and My_Calculation are created in the above example. The methods addition() and Subtraction() are inherited from the class calculation into the My_Calculation. Keyword extends has been used for inheriting the methods.

The compilation and execution of the above code give the following output.

Figure 2: The output of the code in Figure 1

Source

The creation of the My_Calculation class copies the contents of the superclass into the sub-class. With the objects of the subclass, the users can access the subclass member.

In certain cases when we have to differentiate between the members of the sub-class to that of the superclass having the same names, the keyword “super” is used. If supposedly we have two classes Sub_class and Super_class having the same method i.e. display(). The implementation of the method is different but has the same name. In such a case, to differentiate between the method superclass from that of the sub-class, the super keyword has to be used.

Types of Java Inheritance

The different types of inheritance are observed in Java:

1. Single level inheritance

As the name suggests, this type of inheritance occurs for only a single class. Only one class is derived from the parent class. The flow diagram of a single inheritance is shown below:

Figure 3: Graphical illustration of single-level inheritance

Source

Two classes Class A and Class B are shown in Figure 3, where Class B inherits the properties of Class A.

Figure 4: An example of a code applying single-level inheritance

Source

2. Multi-level Inheritance

The multi-level inheritance includes the involvement of at least two or more than two classes. One class inherits the features from a parent class and the newly created sub-class becomes the base class for another new class.

Figure 5: A flow diagram of multi-level inheritance

Source

From the flow diagram in Figure 5, we can observe that Class B is a derived class from Class A, and Class C is further derived from Class B. Therefore the concept of grandparent class comes into existence in multi-level inheritance.  However, the members of a grandparent’s class cannot be directly accessed in Java.

Therefore, Figure 5 shows that Class C is inheriting the methods and properties of both Class A and Class B.

An example of multi-level inheritance is shown below with three classes X, Y, and Z. The class Y is derived from class X which further creates class Z. 

Figure 6: An example of multi-level inheritance

Source

3. Hierarchical Inheritance

The type of inheritance where many subclasses inherit from one single class is known as Hierarchical Inheritance. 

Figure 7: Graphical representation of a hierarchical inheritance.

In Figure 7, we can observe that the three classes Class B, Class C, and Class D are inherited from the single Class A. All the child classes have the same parent class in hierarchical inheritance.

Figure 8: An example of code showing the concept of hierarchical inheritance

Source

The above code produces the output

meowing…

eating…

Other than these types of inheritance in Java, there are other types known as multiple inheritances and hybrid inheritance. Both types are not supported through classes and can be achieved only through the use of interfaces. 

4. Multiple Inheritance

Multiple inheritances is a type of inheritance where a subclass can inherit features from more than one parent class. 

Figure 9: Representation of multiple inheritances

Source

Figure 4 shows that Class C is derived from the two classes Class A and Class B. In other words it can be described that subclass C inherits properties from both Class A and B.

5. Hybrid Inheritance

Hybrid inheritance is a type of inheritance that combines single inheritance and multiple inheritances. As multiple inheritances is not supported by Java, hybrid inheritance can also be achieved through interfaces only.

Figure 10: Representation of a hybrid inheritance

Source

With the different types of inheritance in Java, the ultimate aim is to create sub-classes having properties inherited from the superclasses. The created sub-classes have various properties which are:

  • Fields and methods inherited in a subclass can be directly used.
  • New fields and methods can also be declared in the subclass which is not present in the superclass.
  • A new instance method can be created in the subclass having the same signature as the method in the superclass. The process is referred to as overriding.
  • A new static method can be created in the subclass having the same signature as the method in the superclass. The process is referred to as hiding.

Conclusion

The article discussed the important concept of inheritance in Java and the types of inheritance in Java. Inheritance is therefore the mechanism where we can reuse the codes in order to acquire the properties of a class into another class. This can be achieved through the different types of inheritance patterns as shown in the article. However, there is much more to the concept of inheritance. To excel in the field of development, mastering in the complex programming of Java is required. If you have a dream of innovating intelligent devices, then Executive PG Program in Full-stack Software Development offered by upGrad’s would be the best choice. Certified from the Liverpool John Moores University, the course in association with IIIT Bangalore offers 500+ learning hours and is designed for early professionals. The skill learned from the upGrad’s course will help in opening up opportunities towards the field of software development, web development, javascript developer, etc. Open to any gender within the age group of 21-45, the interactive program might be the best choice for all the coders.

UPGRAD AND IIIT-BANGALORE'S EXECUTIVE PG PROGRAM IN SOFTWARE DEVELOPMENT
Apply Now

0 replies on “Types of Inheritance in Java: Single, Multiple, Multilevel & Hybrid”

Accelerate Your Career with upGrad

Our Popular Software Engineering Courses

×