Inheritance is one of the core pillars of Object-Oriented Programming (OOP) in Java. It allows you to create new classes based on existing ones, promoting code reuse and polymorphism.
π What is Inheritance?
- Definition: Inheritance is a mechanism where one class (child/subclass) acquires the properties and behaviors of another class (parent/superclass).
- Why it matters: Reduces code duplication, improves maintainability, and enables polymorphic behavior.
- When to use: When multiple classes share common behavior or structure.
[Related: link-to-other-article]
πΉ The extends Keyword
- Used to define a subclass that inherits from a superclass.
- Java supports single inheritance (one parent class).
π» Example:
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound(); // Inherited from Animal
d.bark(); // Dog's own method
}
}
πΉ Method Overriding
- Definition: When a subclass provides its own implementation of a method already defined in the parent class.
- Rules:
- Method name and parameters must match exactly.
- Access modifier cannot be more restrictive.
- Use
@Overrideannotation to avoid mistakes.
π» Example:
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
πΉ Real-World Analogy
Think of inheritance like a family tree. A child inherits traits from parents (fields/methods) but can also override certain behaviors (method overriding).
π« Common Mistakes and Anti-Patterns
- β Using inheritance for code reuse without proper IS-A relationship.
- β Forgetting to use
@Override, leading to method overloading instead of overriding. - β Overusing inheritance instead of composition.
π Performance and Memory Implications
- Slight overhead in method lookups for overridden methods due to dynamic binding.
- Reduces code duplication, saving memory in large projects.
| Aspect | Impact |
|---|---|
| Inheritance | Promotes code reuse |
| Method Overriding | Enables runtime polymorphism |
π§ Best Practices
- Use
@Overridefor clarity and safety. - Favor composition over inheritance when classes donβt share a true IS-A relationship.
- Keep inheritance hierarchies shallow to maintain readability.
π Interview Questions
-
Q: Can constructors be inherited?
A: No, constructors are not inherited but can be called viasuper(). -
Q: Whatβs the difference between method overloading and overriding?
A: Overloading is compile-time polymorphism; overriding is runtime polymorphism. -
Q: Can a subclass override a private method?
A: No, private methods are not visible to subclasses.
π Java Version Relevance
| Java Version | Change |
|---|---|
| Java 1.0 | Inheritance and extends introduced |
| Java 5 | @Override annotation introduced |
β Conclusion & Key Takeaways
- Inheritance enables code reuse and polymorphism.
- Use
extendsfor subclassing and@Overrideto customize behavior. - Avoid deep hierarchies and use composition when more appropriate.
β FAQ
Q: Does Java support multiple inheritance?
A: No, but you can achieve similar behavior using interfaces.
Q: Can we prevent a class from being inherited?
A: Yes, by declaring it final.
Q: Can static methods be overridden?
A: No, they are hidden, not overridden.