What is inheritance in Java?

Inheritance in Java is basically a mechanism in which an object obtains every property as well as the behaviors of the parent object. It is an important part of an object oriented programming system (OOPS). Inheritance in Java allows you to create new classes that are already built on existing classes. Since you inherit from an existing class, the methods, as well as the field from the parent class, can be reused. Apart from that, new methods and fields can also be added to the existing class. Inheritance means the IS-A relationship, which, in other words, is also known as the parent-child relationship.

Why is inheritance in Java used?

Inheritance in Java is used for two reasons:

  • Method replacement
  • Reusability of the code.

Terms used in Heritage

To classify: The class is a collection of objects with common properties. You can also call it a plane or a model from which you can create objects.

Subclass: The subclass is also called a child class which inherits from another class. Other names for this class are extended class or derived class.

Super-class: Often referred to as the parent class, this is the class from which the subclass inherits many features. Other names for this class are parent class or base class.

Reusability: The name explains it all. Reusability is a mechanism that helps you reuse the methods as well as the fields of the current class while you create a new class. The same fields, as well as the methods already explained in the previous 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

ten

superclass class

{

// superclass of data variables

// member functions of the superclass

}

class subclass extends superclass

{

// subclass of data variables

// member functions of the subclass

}

 

The keyword “extends” means that you are creating a new class that comes from a current class. Extending here means increasing functionality.

Types of inheritance in Java

The different types of inheritance in Java understand:

  • Unique heritage
  • Multiple inheritance
  • Multi-level inheritance
  • Hierarchical inheritance
  • Hybrid inheritance

Unique heritage

Single inheritance means creating subclasses from a single base. In inheritance, we have access to superclass methods as well as to variables. Subclass methods and variables are also accessible through subclass objects. The superclass and subclass methods must be taken into account and the variable names must not conflict.

Example:

1

2

3

4

5

6

7

8

9

ten

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

Class A

{

int a, b;

empty display ()

{

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

}

}

class B extends A

{

int c;

null show ()

{

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

}

SingleInheritance class

{

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 class derived from several base classes. In such a case, there are several superclasses 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 cases, Java developers try to use multiple inheritances. Java developers are used to the concept of interfaces and expect developers to have multiple legacies by using multiple interfaces.

Example:

The Myclass class implements interface1, interface2

Multi-level inheritance

Multi-level inheritance means a class that extends to the other class that has already been extended from the other class. Take, for example, if there is a class A that extends to class B, and there is a class B that extends further from class C, the scenario is simply following multi-inheritance. levels. We can also take the example of three classes, which include the Vehicle class, the SUV class and the Car class. Here, the Vehicle class is considered the grandfather class. The Car class extends to the Vehicle class, then the SUV class extends to the Car class.

Hierarchical inheritance

According to hierarchical inheritance, a single base class is extended by several derived classes. To put it simply, this means that a single parent class is extended by multiple child classes derived from the parent.

For example, in the case of smartphones of the parent class and of the child classes of Apple, Samsung and Nokia. In the hierarchical inheritance of Java, the Apple class, the Samsung class, and the Nokia class all extend the Smartphone class.

Hybrid inheritance

It is a combination of inheritances. In hybrid inheritance, several types of inheritance are observed. Take, for example, classes like class A and class B, which extend to class C, and then there’s also class D, which actually extends to class A. This type of inheritance is called hybrid inheritance.

Why does Java not support multiple inheritance?

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

This concludes our discussion of inheritance in Java. Hope this article helps you a lot to understand this topic. If you are interested in free online courses with certificatesSo sign up for Great Learning Program today.


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