Inheritance:

Java Inheritance is one of the main pillar in Object Oriented Programming language. Inheritance stands as one of the foundational concepts in the realm of Java programming. It’s a mechanism that empowers developers to create new classes by inheriting the attributes and methods of pre-existing classes. This inheritance principle forms the bedrock of code reusability and helps maintain a well-structured class hierarchy within a Java program. Java offers various forms of inheritance, encompassing single, multiple, multilevel, and hierarchical inheritance. In this comprehensive article, we’ll delve into these diverse types of inheritance and provide a hands-on Java program to illustrate how inheritance operates in practice.

Understanding Java Inheritance:

At its core, inheritance represents an “is-a” relationship between classes. A derived class, often referred to as a subclass, inherits the instance variables and methods of its superclass or base class. This means that a subclass can not only access but also extend the functionality of its superclass.

Types of Java Inheritance:

Let’s explore the various types of inheritance that Java supports:

Single Inheritance:

In single inheritance, a subclass can have only one superclass. This implies that a class inherits from just a single parent class.

Java Inheritance

Multiple Inheritance:

In multiple inheritance, a subclass can have multiple superclasses. This allows a class to inherit properties and methods from several parent classes. However, it’s important to note that Java does not support multiple inheritance directly to maintain code simplicity and avoid complexities.

Java Inheritance - Multiple Inheritance

Multilevel Inheritance:

Multilevel inheritance occurs when a class is derived from another subclass. In this scenario, class hierarchies can extend multiple levels deep. One best example for multilevel inheritance is SON, Father and Grand Father (Father’s Father)

Java Inheritance - Multilevel Inheritance

Hierarchical Inheritance:

Hierarchical inheritance involves one superclass having multiple subclasses. Each subclass inherits from the same superclass, forming a hierarchical structure.

Java Inheritance - Heirarchical Inheritance
Java Inheritance

Java’s Approach to Inheritance:

While Java supports various forms of inheritance, it deliberately avoids allowing multiple inheritance. The reason behind this choice is to keep Java as a simple and comprehensible programming language. Multiple inheritance can introduce complications and ambiguities in the code, making it harder to understand and maintain.

Java Inheritance – Code Reusability

Java Inheritance simplifies the development process by allowing the child class to inherit methods and data members from its parent class. This powerful mechanism eliminates the redundancy of writing the same code in the child class, making development more efficient and saving valuable time.

To understand Java Inheritance better, let’s delve into the essential principles that govern access to a parent class. The accessibility of a parent class is determined by the access modifiers employed in its definition. These access modifiers play a crucial role in controlling how child classes can interact with and inherit from their parent classes.

Demonstrating Java Inheritance:

To illustrate how inheritance works in Java, let’s consider a practical example:

class Vehicle {
String typeOfVehicle;
}

public class Car extends Vehicle {
String modelType;

public void showDetail() {
    typeOfVehicle = "Car"; // Accessing the Vehicle class member
    modelType = "sports";
    System.out.println(modelType + " " + typeOfVehicle);
}

public static void main(String[] args) {
    Car car = new Car();
    car.showDetail();
}

}

Output of the Program:

Sports Car

In this example, we have a Vehicle class and a Car class that extends the Vehicle class. The Car class inherits the typeOfVehicle variable from Vehicle and adds its modelType variable. The showDetail method sets the vehicle type and model and then displays the result. When we run the program, it prints “Sports Car.”

In conclusion, inheritance is a crucial concept in Java, enabling code reuse and facilitating the creation of well-structured class hierarchies. Understanding the different types of inheritance and how they work is essential for effective Java programming.

Java tutorials ad other article on Java is here.

Java Inheritance official documentation.

About Santosh Kumar Gadagamma

I'm Santosh Gadagamma, an Experienced Software Engineer passionate about sharing knowledge in technologies like Java, C/C++, DBMS/RDBMS, Bootstrap, Big Data, Javascript, Android, Spring, Hibernate, Struts, and all levels of software design, development, deployment, and maintenance. I believe computers are essential for the world's functioning, and I'm committed to helping others learn the skills they need to succeed in tech. My website is a valuable learning tool to help you reach greater heights in your education and career, and I believe that education has no end points.

Check Also

String Builder

String Builder : A Comprehensive Guide

String builder in Java is a class. It is like string objects, except that they …