Inheritance is one of the core concepts of Object-Oriented Programming (OOP) in Java. It allows one class (called the child or subclass) to inherit the properties and methods of another class (called the parent or superclass). This promotes code reuse, modularity, and easier maintenance.
Key Features of Inheritance in Java
- Code Reusability: A subclass can reuse the code defined in its superclass.
- Extensibility: Subclasses can add their own methods and fields, extending the functionality of the superclass.
- Method Overriding: A subclass can provide a specific implementation of a method defined in the superclass.
- IS-A Relationship: Inheritance represents an “is-a” relationship (e.g., a Dog is a Animal).
Syntax of Inheritance
To inherit a class, use the extends
keyword:
class Superclass {
// Fields and methods
}
class Subclass extends Superclass {
// Additional fields and methods
}
Example of Single Inheritance
// Superclass
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
// Subclass
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 from Dog class
}
}
Output:
This animal eats food.
The dog barks.
Types of Inheritance in Java
- Single Inheritance:
- One subclass inherits from one superclass.
- Example:
Dog
inherits fromAnimal
.
- Multilevel Inheritance:
- A class inherits from another class, which in turn inherits from a third class.
class Animal {} class Mammal extends Animal {} class Dog extends Mammal {}
- Hierarchical Inheritance:
- Multiple classes inherit from the same superclass.
class Animal {} class Dog extends Animal {} class Cat extends Animal {}
- Hybrid Inheritance:
- A combination of two or more types of inheritance. This is not supported directly in Java due to the ambiguity caused by multiple inheritance.
Key Concepts in Java Inheritance
1. Method Overriding
A subclass can provide its own implementation of a method defined in its superclass:
class Animal {
void sound() {
System.out.println("Some generic animal sound.");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks.");
}
}
2. super
Keyword
- Used to access members of the superclass.
- Examples:
- Call superclass constructor:
super();
- Access superclass methods:
super.methodName();
- Call superclass constructor:
3. Final Keyword
- If a class is marked as
final
, it cannot be extended. - If a method is marked as
final
, it cannot be overridden.
4. Object Class
- Every class in Java implicitly extends the
Object
class, which is the root of the class hierarchy.
Advantages of Inheritance
- Reusability: Code can be reused across multiple classes.
- Modularity: Common behavior is centralized in the superclass.
- Extensibility: Easy to add new functionalities.
Limitations of Inheritance
- Tight Coupling: Changes in the superclass can affect all subclasses.
- Single Inheritance Only: Java does not support multiple inheritance to avoid ambiguity (solved with interfaces).
- Overhead: Improper use of inheritance can lead to unnecessary complexity.
Conclusion
Inheritance in Java is a powerful mechanism that enhances code reuse and organization. By understanding how to implement and use inheritance properly, you can write efficient, scalable, and maintainable object-oriented programs.