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 in Java
- How inheritance works in Java
- Step‑by‑step Java examples
- Using the
superkeyword - Real‑world OOP scenarios
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.
Multiple Inheritance
While Java itself does not allow a class to extend multiple classes, understanding the concept helps when using interfaces.
Multilevel Inheritance
Inheritance chains can span several levels: a subclass can itself be a superclass for another class.
Hierarchical Inheritance
One superclass can give rise to multiple subclasses.
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.
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.
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.
Using OOP, you define a base Account class containing shared behavior, and extend it for specific account types.
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.
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.
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
- Mastering Java Inheritance: Concepts, Types, and Practical Examples
- Encapsulation in Java: A Comprehensive Guide with Practical Example
- Java Variables and Data Types – A Comprehensive Guide with Examples
- Understanding Java Classes and Objects: Clear Concepts, Practical Examples
- Java HashMap: A Comprehensive Guide
- Polymorphism in Java: A Comprehensive Guide with Practical Examples
- Java Abstraction: Mastering Abstract Classes, Methods, and Practical Examples
- Java Interfaces Explained: How to Define and Implement Them with Practical Examples
- Master Java Reflection API: A Practical Guide with Code Examples
- Python OOP Fundamentals: Classes, Objects, Inheritance, and Constructors Explained