What Are the Different Types of Inheritance in Java?

Introduction 

Java is an object-oriented programming language that is widely used to create websites, apps, and other digital content. In object-oriented programming, the concept of inheritance describes an object’s capacity to adopt one or more traits from other classes of objects. Instance variables or member functions are frequently inheritances. Subclasses are objects that inherit these traits from their parent class. A superclass is an object from which they are inherited. However, the specifics of inheritance implementation differ from language to language.  

The most crucial aspect of oops as object-oriented programming in Java is inheritance. It enables us to use properties from one class’s inheritance in another class. Through the usage of public classes and interfaces, programmers can independently modify original software by building new classes on top of pre-existing ones, specifying a new implementation while preserving the same behaviors (realizing an interface). 

Java has been one of the most popular programming languages and platforms for so long, and it is arguably older than some of the engineers who use it. According to the StackOverflow Developer Survey, it is still in the top five. 

What is Inheritance? 

A fundamental idea in Java is inheritance, which calls for passing on a class’s properties to another class, such as a guardian. Take the father-son connection as an example. Alternately, we may define inheritance as the properties that are derived from one class to another. The class receives shared techniques and properties from another class. Data in a program can be arranged in time order using Java’s inheritance techniques. 

In Java, inheritance enables stoners to create classes and properties specifically for their purposes. To your knowledge, it was a programming language developed by an Oslo computer center for the Simula in 1969. Although it is available in all object-oriented programming languages, including PHP, C, and Python, we will discuss it here in terms of Java. Both class and prototype are specifically referred to as inheriting. We will read more about Java’s types of inheritance. 

Important Terms in Inheritance 

Terminology for the Inheritance  

  1. Class: A class is a collection of objects with similar attributes. It serves as a model or blueprint from which things can be made. 
  1. Sub Class/Child Class: A class that inherits from another class is referred to as a subclass. A derived class, extended class, or child class are other names for it. 
  1. Super Class/Parent Class: The class from which a subclass gets its features is referred to as the superclass or parent class. It is also referred to as a parent class or a base class. 
  1. Reusability: Reusability is a feature that enables you to reuse the fields and methods of an existing class when you build a new class, as the name implies. The same fields and methods described in the preceding class may be used. 

How Many Types of Inheritance Are There In Java? 

Five types of inheritance are present in Java. Let’s discuss each of them in detail. 

1. Single Inheritance:  

Subclasses inherit properties from a single superclass using a process known as single inheritance. As we might say, single inheritance is the process of creating a parent class from a single base class because the superclass can be created with just one class. We can use the variables from superclasses and subclasses’ methods and attributes without running afoul of each other in inheritance.  

2. Multiple Inheritance:  

A subclass may have more than one superclass and may inherit traits from each of them at once. Multiple inheritances refer to a chain of different inheritances. Multiple inheritances is the process of simplifying derived classes from multiple classes. Therefore, at the very least, there may be one class here or, more than that, additional classes of a superclass. Java does not support multiple inheritances, although object-oriented programming does.  

Java programmers are constantly intrigued by using multiple inheritances under certain circumstances. Java programmers employ interface ideas to implement multiple inheritances. Why is Java not capable of supporting multiple inheritances? This is being done for the straightforward purpose of eliminating ambiguity. Whenever class A is extended to class B and class A, class C uses the same methodology as class B. 

3. Multilevel Inheritance:  

A subclass may have additional subclasses of its own via multilevel inheritance. In other words, a superclass’s subclasses can themselves be superclasses to other subclasses. In Java, multilevel inheritance extends a class that has already been extended and adds two classes, if not more. Multilevel inheritance, for example, occurs when class A extends from a class B, which in turn extends from a class C. A graph is provided below to help you understand. We can use classes as an example, such as class “food,” class “pizza,” and class “coca-cola,” where class “food” serves as the parent class and class “pizza” extends to class “coca-cola.” In Java, there are different types of inheritance. 

4. Hierarchical inheritance:  

A base class serves as the parent superclass to several tiers of subclasses in a hierarchy of inheritance. Hierarchical inheritance in Java refers to the process wherein two child classes extend a single parent class. Hierarchical inheritance is also referred to as when a class with a single parent has a class with two children. 

Here are some examples: Potato, tomato, and cucumber are considered children of the parent class of vegetables, which is vegetables. Class Potato, Class Tomato, and Class Cucumber are the extended class of veggies in this Java Hierarchical Inheritance. In Java, it also comes in several forms of inheritance. 

5. Hybrid inheritance:  

Combining one or more of the other inheritance types results in hybrid inheritance. In Java, a combination of inheritance is referred to as hybrid inheritance. This form of inheritance combines both single inheritances and multiple inheritances. Since hybrid inheritance is not enabled in Java, it can also be used through interfaces in this case. Java uses hybrid inheritance to offer code reuse and to modularize the software into clearly defined classes. For example, Inheritance combines single and hierarchical inheritance, such as when class A and class B extend class C and another class D extends class A. 

Why is Java Unable to Allow Multiple Inheritance? 

Multiple inheritances are not enabled in Java in order to simplify the language and reduce its complexity. Imagine that there are three classes: A, B, and C. A and B classes are inherited by the C class. There will be confusion over whether to invoke the method of class A or class B if both A and B have the same method and you call it from an object of the child class. 

Java generates a compile-time error if you inherit two classes because compile-time errors are preferable to runtime errors. There will be a build time error whether you use the same or a different approach. 

Inheritance In Java 

One object can acquire all a parent object’s properties and actions through the technique of inheritance in Java. It is a crucial component of OOPs (Object Oriented programming systems). The concept of inheritance in Java is that new classes can be constructed on top of older ones. You can use the parent class’s methods and properties when you inherit from an existing class. You can also add additional fields and methods to your existing class. 

 

The parent-child relationship, also known as the IS-A relationship, is represented by inheritance. The existing class from which the child class is derived is known as the superclass, and the newly produced class is known as the subclass (child or derived class) (parent or base class). 

Java’s extends keyword is used to carry out inheritance. For instance, 

class Animal { 

// methods and fields 

} 

 

// use of extends keyword 

// to perform inheritance 

class Dog extends Animal { 

 

// methods and fields of Animal 

// methods and fields of Dog 

} 

 

The Dog class in the previous example was constructed by inheriting the properties and methods from the Animal class. “Dog” is the subclass in this instance, whereas “Animal” is the superclass. 

What is the purpose of inheritance? 

Through the usage of public classes and interfaces, programmers can independently modify original software by building new classes on top of pre-existing ones, specifying a new implementation while preserving the same behaviors (realizing an interface). Code can be combined and reused by using inheritance. A vehicle superclass can be created if, for instance, the objects “car,” “truck,” and “motorcycle” are all subclasses of the vehicle object. Subclasses immediately inherit this code and any upcoming modifications. 

Java Inheritance Example 

In the below example of inheritance, class Car is a base class, class MahindraThar is a derived class that extends Car class and class Test is a driver class to run the program. 

// base class 

class Car { 

          // the Car class has two fields 

          public int gear; 

          public int speed; 

 

          // the Car class has one constructor 

          public Car(int gear, int speed) 

          { 

                    this.gear = gear; 

                    this.speed = speed; 

          } 

 

          // the Car class has three methods 

          public void applyBrake(int decrement) 

          { 

                    speed -= decrement; 

          } 

 

          public void speedUp(int increment) 

          { 

                    speed = increment; 

          } 

 

          // toString() method to print info of Car 

          public String toString() 

          { 

                    return (“No of gear are ” gear “\n” 

                                         “speed of car is ” speed); 

          } 

} 

 

// derived class 

class MahindraThar extends Car { 

 

          // the MahindraThar subclass adds one more field 

          public int Height; 

 

          // the MahindraThar subclass has one constructor 

          public MahindraThar(int gear, int speed, int startHeight) 

          { 

                    // invoking base-class(Car) constructor 

                    super(gear, speed); 

                    Height = startHeight; 

          } 

 

          // the MahindraThar subclass adds one more method 

          public void setHeight(int newValue) 

          { 

                    Height = newValue; 

          } 

 

          // overriding toString() method 

          // of Car to print more info 

          @Override public String toString() 

          { 

                    return (super.toString() “\nheight is “ 

                                         Height); 

          } 

} 

 

// driver class 

public class Test { 

          public static void main(String args[]) 

          { 

 

                    MahindraThar mt = new MahindraThar (30, 1000, 250); 

                    System.out.println(mt.toString()); 

          } 

} 

 

Output: 

No of the gears are 30 

speed of the car is 1000 

height is 250 

In the program above, whenever a MahindraThar class object is formed, a copy of all the superclass’s methods and fields is loaded into memory. Because of this, we can access a superclass’s members using the subclass’s object. 

Please be aware that just the subclass’s object—not the superclass’s—is produced during inheritance. 

Is-a relationship in Java 

In Java, an is-a connection is what inheritance is. In other words, we only use inheritance when a relationship of the kind “is-a” exists between two classes. For instance, 

A car is a Vehicle 

Orange is a Fruit 

A surgeon is a Doctor 

A dog is an Animal 

Here, a Car can inherit from Vehicle, an Orange can inherit from Fruit, and so on. 

Conclusion 

One of the fundamental concepts of OOP is inheritance, which enables us to derive a new class from an existing one. We have covered all aspects of inheritance in Java, including the key types of inheritance. Given a Java coding example, starting with an introduction to the class and the application. Therefore, inheritance is the method we will use to reuse the scripts and add the properties of a category to another class. We can now confidently state that we have increased our knowledge of Java inheritance structures, including single, multiple, multi-level, hierarchical, and hybrid inheritance. 

Related Articles

} }
Request Callback