Inheritance in Java
Inheritance is one of the main concepts of Object-Oriented Programming (OOP). It allows one class to acquire the properties and methods of another class.
The class whose properties are inherited is called the parent class (superclass), and the class that inherits them is called the child class (subclass).
Inheritance is implemented using the extends keyword.
Syntax
class Parent { // properties and methods } class Child extends Parent { // additional properties and methods }
Example
class Animal { void eat() { System.out.println("Animal is eating"); } } class Dog extends Animal { void bark() { System.out.println("Dog is barking"); } public static void main(String[] args) { Dog d = new Dog(); d.eat(); d.bark(); } }
Output
Animal is eating Dog is barking
Here, Dog inherits the eat() method from the Animal class.
Types of Inheritance
Java supports the following types of inheritance through classes:
1. Single Inheritance
One child class inherits from one parent class.
Animal | Dog
class Animal { void eat() { System.out.println("Eating"); } } class Dog extends Animal { void bark() { System.out.println("Barking"); } }
2. Multilevel Inheritance
A class inherits from another child class, forming a chain.
Animal | Dog | Puppy
class Animal { void eat() { System.out.println("Eating"); } } class Dog extends Animal { void bark() { System.out.println("Barking"); } } class Puppy extends Dog { void play() { System.out.println("Playing"); } }
3. Hierarchical Inheritance
Multiple child classes inherit from the same parent class.
Animal / \ Dog Cat
class Animal { void eat() { System.out.println("Eating"); } } class Dog extends Animal { void bark() { System.out.println("Barking"); } } class Cat extends Animal { void meow() { System.out.println("Meowing"); } }
Multiple Inheritance
Java does not support multiple inheritance using classes.
For example:
A B \ / C
Java avoids this type of class inheritance because it can create ambiguity when both parent classes contain methods with the same name.
However, Java can achieve multiple inheritance through interfaces.
interface A { void show(); } interface B { void display(); } class C implements A, B { public void show() { System.out.println("A"); } public void display() { System.out.println("B"); } }
Advantages of Inheritance
- Code Reusability – Reuses existing code.
- Reduces Code Duplication – Common functionality can be placed in the parent class.
- Method Overriding – Supports runtime polymorphism.
- Easy Maintenance – Common code can be maintained in one place.
- Extensibility – Child classes can add new features.
Important Keywords
| Keyword | Purpose |
|---|---|
extends | Used for class inheritance |
implements | Used when a class implements an interface |
super | Refers to the immediate parent class |
this | Refers to the current object |
Key Points
- Inheritance represents an "is-a" relationship.
- It promotes code reuse.
- A child class can use accessible members of its parent class.
- Java supports single, multilevel, and hierarchical inheritance through classes.
- Multiple inheritance is not supported through classes.
- Multiple inheritance can be achieved using interfaces.
Conclusion
Inheritance allows a child class to reuse and extend the functionality of a parent class. It is an important OOP feature that promotes code reusability, maintainability, extensibility, and runtime polymorphism in Java.