Inheritance in Java

📘 Java 👁 56 views 📅 Dec 01, 2025
⏱ Estimated reading time: 2 min

Inheritance is a fundamental concept of object-oriented programming (OOP) in Java. It allows one class (child/subclass) to acquire the properties and behaviors of another class (parent/superclass). Inheritance promotes code reuse, modularity, and extensibility.


1. Key Features of Inheritance

  • Enables a new class to reuse code from an existing class.

  • Supports hierarchical organization of classes.

  • Reduces redundancy and improves maintainability.

  • Facilitates polymorphism in Java.


2. Types of Inheritance in Java

(a) Single Inheritance

A subclass inherits from one superclass.

class Animal { void eat() { System.out.println("Eating..."); } } class Dog extends Animal { void bark() { System.out.println("Barking..."); } } public class Test { public static void main(String[] args) { Dog d = new Dog(); d.eat(); // Inherited method d.bark(); } }

(b) Multilevel Inheritance

A class inherits from a subclass, forming a chain of inheritance.

class Animal { void eat(){} } class Mammal extends Animal { void walk(){} } class Dog extends Mammal { void bark(){} }

(c) Hierarchical Inheritance

Multiple subclasses inherit from a single superclass.

class Animal { void eat(){} } class Dog extends Animal { void bark(){} } class Cat extends Animal { void meow(){} }

(d) Hybrid Inheritance

Combination of two or more types of inheritance.
Java does not support multiple class inheritance directly but supports it via interfaces.


3. The super Keyword

  • Refers to the parent class object.

  • Used to access parent class methods, variables, and constructors.

class Animal { void eat() { System.out.println("Eating..."); } } class Dog extends Animal { void eat() { System.out.println("Dog Eating..."); } void show() { super.eat(); } }

4. Advantages of Inheritance

  • Code Reusability: Avoids writing duplicate code.

  • Method Overriding: Enables polymorphism.

  • Organized Structure: Easier maintenance and readability.

  • Extensibility: New features can be added without changing existing code.


5. Conclusion

Inheritance in Java allows a class to inherit properties and behaviors from another class, supporting the OOP principles of reusability, modularity, and extensibility. Proper use of inheritance simplifies program design and improves maintainability, making it a core concept in Java programming.


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes