What is Inheritance in Java?

Inheritance in Java is basically a mechanism in which an object obtains each property as well as the behaviors of the parent object. It is a significant part of an Object-Oriented programming system (OOPS). The inheritance in Java enables you to create new classes which are already built on existing classes. As you inherit from an existing class, methods, as well as the field of the parent class, can be reused. Apart from this, new methods and fields can also be added to the existing class. Inheritance signifies the IS-A relation, which in other words, is also called a parent-child relation.

Why inheritance in Java is used?

Inheritance in Java is used for two reasons:

  • Method Overriding
  • Code Reusability.

Terms that are used in Inheritance

Class: Class is a collection of objects that come with common properties. You can also call it a blueprint or template from which you can create objects.

Sub-class: Sub-class is also called a child class which inherits another class. The other names for this class are extended class or derived class.

Super-class: Often called parent class, it is the class from where the subclass inherits many features. The other names for this class are parent class or base class.

Reusability: The name explains it all. Reusability is a mechanism that helps you to reuse the methods as well as fields of the current class during the time you create a new class. The same fields, as well as methods that are already explained in the earlier class, can be used.

The syntax of Java Inheritance

class Subclass-name extends Superclass-name

{

//methods and fields

}

General format for Inheritance

1

2

3

4

5

6

7

8

9

10

class​ superclass

{

// superclass data variables

// superclass member functions

}

class​ subclass ​extends​ superclass

{

// subclass data variables

// subclass member functions

}

 

The keyword “extends” signifies that you are creating a new class that originates from a current class. Extends here means increasing the functionality.

Types of Inheritance in Java

The various types of inheritance in Java include:

  • Single Inheritance
  • Multiple Inheritance
  • Multi-Level Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

Single Inheritance

Single inheritance means creating subclasses from a single base. In inheritance, we get access to superclass methods as well as variables. Subclass methods and variables can also be accessed through subclass objects. The superclass and subclass methods need to be taken care of, and the names of variables must not conflict.

Example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

class A

{

int a, b;

void display()

{

System.out.println(“Inside class A values =”+a+” ”+b);

}

}

class B extends A

{

int c;

void show()

{

System.out.println(“Inside Class B values=”+a+” “+b+” “+c);  }

}

class SingleInheritance

{

public static void main(String args[])

{

B obj = new B(); //derived class object

obj.a=10;

obj.b=20;

obj.c=30;

obj.display();

obj.show();

}

}

 

Multiple Inheritance

Multiple Inheritance means describing a derived class from several base classes. In one such case, there is more than one superclass, and the subclasses can be one or more. Multiple inheritances can be found in object-oriented programming with C++ but are not available in Java.

In some of the cases, the Java developers try to use multiple inheritances. The Java developers are accustomed to the concept of interface and expect the developers to have multiple inheritances by using multiple interfaces.

Example:

Class Myclass implements interface1, interface2

Multilevel Inheritance

Multilevel Inheritance means a class that extends to the other class which has already been extended from the other class. Taking, for instance, if there is class A which extends to class B, and there is class B that further extends from class C, the scenario simply follows multilevel inheritance. We can also take the example of three classes, which include class Vehicle, class SUV, and class Car. Here, the class Vehicle is considered the grandfather class. The class Car extends to class Vehicle, and then the class SUV extends to class Car.

Hierarchical Inheritance

According to the Hierarchical Inheritance, a single base class is extended by more than one derived class. To put it simply, it means that a single parent class is extended by multiple child classes derived from the parent.

For example, in the case of parent class Smartphones and the child classes of Apple, Samsung, and Nokia. In Java’s Hierarchical Inheritance, class Apple, class Samsung, and class Nokia all extend the class Smartphone.

Hybrid Inheritance

It is a combination of inheritances. In Hybrid Inheritance, more than one type of inheritance is seen. Taking, for example, we have classes such as class A and class B, which extend to class C, and then there is also class D, which actually extends to class A. This kind of inheritance is called Hybrid Inheritance.

Why Java does not support Multiple Inheritance?

Multiple Inheritance is not supported in Java and lets us explain the reason for it with an example. Let us consider three classes by the name of class X, class Y, and class Z. Here the class Z extends both class X and class Y. In this case, let us consider that a method print() is a part of class X as well as class Y; however, print() in class X is different from print() in class Y. Now when the inheritance is taking place, the compiler cannot decide between print() from which class it should inherit. To avoid such a circumstance, Java does not support multiple inheritances.

This concludes our discussion on inheritance in Java. Hope this article helps you a lot for understanding this topic. If you’re interested in free online courses with certificates, So enroll today on Great Learning Programme.


Interesting Related Article: “How Java Is Revolutionizing The FinTech Industry