Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java Inheritance Explained: Types, Syntax, and Practical Examples

What Is Inheritance?

Inheritance is a core object‑oriented concept that allows one class to acquire the fields and methods of another. Think of it as a child inheriting traits from its parents. By reusing existing code, inheritance drives reusability and simplifies maintenance.

This guide covers:

Types of Inheritance

Java supports several inheritance patterns, each suited to different design needs.

Single Inheritance

One class extends another, forming a simple parent‑child relationship.

Java Inheritance Explained: Types, Syntax, and Practical Examples
Class B extends Class A.

Multiple Inheritance

While Java itself does not allow a class to extend multiple classes, understanding the concept helps when using interfaces.

Java Inheritance Explained: Types, Syntax, and Practical Examples
Class C extends both Class A and Class B (conceptual).

Multilevel Inheritance

Inheritance chains can span several levels: a subclass can itself be a superclass for another class.

Java Inheritance Explained: Types, Syntax, and Practical Examples
Class C extends B, which extends A.

Hierarchical Inheritance

One superclass can give rise to multiple subclasses.

Java Inheritance Explained: Types, Syntax, and Practical Examples
Classes B, C, and D all extend Class A.

Hybrid Inheritance

Hybrid inheritance combines single and multiple inheritance patterns. Although Java does not support it directly, the concept is useful when designing complex hierarchies using interfaces.

Java Inheritance Explained: Types, Syntax, and Practical Examples
Class D inherits from Class B and Class C, which both extend Class A.

Note: Java does not support hybrid or multiple inheritance for classes.

Inheritance in Java

In Java, an Is‑A relationship is expressed through inheritance. The extends keyword signals that a subclass inherits from a superclass.

Typical syntax:

class SubClass extends SuperClass {
    // methods and fields
}

Java Inheritance Example

Below is a concise illustration of inheritance in action.

Java Inheritance Explained: Types, Syntax, and Practical Examples
Java Inheritance Example
class Doctor {
    void doctorDetails() {
        System.out.println("Doctor Details...");
    }
}

class Surgeon extends Doctor {
    void surgeonDetails() {
        System.out.println("Surgeon Detail...");
    }
}

public class Hospital {
    public static void main(String[] args) {
        Surgeon s = new Surgeon();
        s.doctorDetails();
        s.surgeonDetails();
    }
}

Using the super Keyword

The super keyword provides access to superclass members, analogous to this but for parent class elements.

It can be used with variables, methods, and constructors.

Syntax example:

super.methodName();

Inheritance in Action: A Banking Scenario

Consider a banking application that supports multiple account types. In a structured approach, you would write separate functions for common actions like deposit and withdraw.

Java Inheritance Explained: Types, Syntax, and Practical Examples

Using OOP, you define a base Account class containing shared behavior, and extend it for specific account types.

Java Inheritance Explained: Types, Syntax, and Practical Examples
Structured approach: separate functions for deposit and withdraw.
Java Inheritance Explained: Types, Syntax, and Practical Examples
OOP approach: subclassing reduces duplication.

Adding Overdraft Facility

When the requirement changes to add an overdraft feature, the structured code needs to be altered. In contrast, OOP allows you to create a new subclass that implements the unique withdraw logic without touching the tested base code.

Java Inheritance Explained: Types, Syntax, and Practical Examples
Java Inheritance Explained: Types, Syntax, and Practical Examples
Structured approach: modify existing withdraw function.
Java Inheritance Explained: Types, Syntax, and Practical Examples
OOP approach: add new subclass for overdraft account.

Adding a Credit Card Account

Similarly, introducing a credit card account with unique deposit rules is straightforward in OOP: simply extend the base class with specialized behavior.

Java Inheritance Explained: Types, Syntax, and Practical Examples
Java Inheritance Explained: Types, Syntax, and Practical Examples
Structured approach: adjust existing deposit logic.
Java Inheritance Explained: Types, Syntax, and Practical Examples
OOP approach: create a new subclass with custom deposit logic.

Benefits of Inheritance in OOP

Inheritance eliminates code duplication by placing shared logic in a parent class. Child classes inherit deposit and withdraw methods, reducing maintenance overhead.

Java Inheritance Explained: Types, Syntax, and Practical Examples
Java Inheritance Explained: Types, Syntax, and Practical Examples

Java

  1. Mastering Java Inheritance: Concepts, Types, and Practical Examples
  2. Encapsulation in Java: A Comprehensive Guide with Practical Example
  3. Java Variables and Data Types – A Comprehensive Guide with Examples
  4. Understanding Java Classes and Objects: Clear Concepts, Practical Examples
  5. Java HashMap: A Comprehensive Guide
  6. Polymorphism in Java: A Comprehensive Guide with Practical Examples
  7. Java Abstraction: Mastering Abstract Classes, Methods, and Practical Examples
  8. Java Interfaces Explained: How to Define and Implement Them with Practical Examples
  9. Master Java Reflection API: A Practical Guide with Code Examples
  10. Python OOP Fundamentals: Classes, Objects, Inheritance, and Constructors Explained