Fullstack > Java > 🧬 Inheritance
Inheritance is one of the four fundamental Object-Oriented Programming (OOP) concepts in Java, along with Encapsulation, Abstraction, and Polymorphism. It allows a class (subclass or child class) to inherit properties and behaviors (fields and methods) from another class (superclass or parent class), promoting code reusability and hierarchical relationships.
Advantages of Inheritance
- Code Reusability - Inherited methods and attributes reduce redundant code.
- Improved Maintainability - Changes in the parent class reflect automatically in child classes.
- Encapsulation & Modularity - Inheritance structures large projects into smaller, manageable units.
- Polymorphism Support - Enables method overriding, allowing flexibility and dynamic method invocation.
Types of Inheritance in Java
Java supports the following types of inheritance:
- Single Inheritance - A subclass inherits from one superclass.
- Multilevel Inheritance - A class inherits from another class, forming a hierarchy.
- Hierarchical Inheritance - Multiple child classes inherit from the same parent class.
- Multiple Inheritance with Interfaces - Java does not support multiple inheritance with classes but allows it using interfaces.
Note: Java does not support multiple inheritance with classes to avoid ambiguity (Diamond Problem).
extends Keyword in Java
The extends keyword is used to establish inheritance between two classes. The subclass inherits fields and methods from the superclass.
Example: Single Inheritance
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 dog = new Dog();
dog.eat(); // Inherited method from Animal
dog.bark(); // Method of Dog class
}
}
Example: Multilevel Inheritance
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Mammal extends Animal {
void walk() {
System.out.println("Mammals can walk.");
}
}
class Dog extends Mammal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited from Animal
dog.walk(); // Inherited from Mammal
dog.bark(); // Defined in Dog
}
}
Explanation
- The
Dogclass inherits fromAnimal, allowing access toeat(). - The
Dogclass also introduces its own methodbark(). - In the multilevel example,
Doginherits fromMammal, which itself inherits fromAnimal.
Inheritance simplifies development by reducing redundancy and promoting a structured approach to object-oriented programming.
🔗 Related Topics: