Inheritance is a fundamental concept in Java that allows one class to acquire the properties and behaviors of another class. It promotes code reusability and helps in building a hierarchical classification.
In Java, inheritance is implemented using the extends
keyword.
Why Use Inheritance?
- Code Reusability – Avoids redundant code by using already defined functionality.
- Maintainability – Easier to manage and update the code.
- Extensibility – Enhances the existing functionality without modifying the base class.
- Polymorphism Support – Enables method overriding, making the code more flexible.
Syntax
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Parent { // Superclass (Base class) void show() { System.out.println("This is the Parent class"); } } class Child extends Parent { // Subclass (Derived class) void display() { System.out.println("This is the Child class"); } } public class Main { public static void main(String[] args) { Child obj = new Child(); obj.show(); // Calling method from Parent class obj.display(); // Calling method from Child class } } //Output This is the Parent class This is the Child class |
Here, Child
class inherits the show()
method from the Parent
class.
Types of Inheritance
Single Inheritance
A subclass inherits from a single parent class.
Example: A Dog
class inherits properties from an Animal
class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Animal { void eat() { System.out.println("This animal eats food."); } } class Dog extends Animal { void bark() { System.out.println("The dog barks."); } } public class Main { public static void main(String[] args) { Dog d = new Dog(); d.eat(); // Inherited method d.bark(); // Dog class method } } //Output This animal eats food. The dog barks. |
Multilevel Inheritance
A class inherits from another class, which itself is inherited from another class.
Example: Grandparent → Parent → Child
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class Grandparent { void familyName() { System.out.println("This is the Grandparent class."); } } class Parent extends Grandparent { void parentMethod() { System.out.println("This is the Parent class."); } } class Child extends Parent { void childMethod() { System.out.println("This is the Child class."); } } public class Main { public static void main(String[] args) { Child obj = new Child(); obj.familyName(); // Grandparent method obj.parentMethod(); // Parent method obj.childMethod(); // Child method } } //Output This is the Grandparent class. This is the Parent class. This is the Child class. |
Hierarchical Inheritance
Multiple child classes inherit from a single parent class.
Example: A Car
and Bike
class both inherit from Vehicle
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
class Vehicle { void move() { System.out.println("Vehicles help in transportation."); } } class Car extends Vehicle { void carType() { System.out.println("This is a Car."); } } class Bike extends Vehicle { void bikeType() { System.out.println("This is a Bike."); } } public class Main { public static void main(String[] args) { Car c = new Car(); Bike b = new Bike(); c.move(); // Parent method c.carType(); // Car method b.move(); // Parent method b.bikeType(); // Bike method } } //Output Vehicles help in transportation. This is a Car. Vehicles help in transportation. This is a Bike. |
Why Java Doesn’t Support Multiple Inheritance?
Java does not support multiple inheritance using classes to avoid ambiguity problems (Diamond Problem).
Diamond Problem Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class A { void display() { System.out.println("Class A method"); } } class B { void display() { System.out.println("Class B method"); } } class C extends A, B { // Error! Java does not support multiple inheritance public static void main(String[] args) { C obj = new C(); obj.display(); // Ambiguity: Which display() method to call? } } |
Java supports multiple inheritance using interfaces instead of classes.