- Published on
Mastering Inheritance in OOP - Concepts, Examples, Types & Interview Prep
- π Formal Definition
- π§± Key Characteristics of Inheritance
- π οΈ Basic Java Example
- πͺ Real-Life Analogy
- π Types of Inheritance in Java
- π Java Syntax
- π Method Overriding
- π§ Key Interview Points
- βοΈ Inheritance vs Composition vs Interface
- β Best Practices
- β Interview Questions with Answers
- π Common Use Cases of Inheritance
- π Summary
Inheritance is an object-oriented programming principle where a class acquires properties and behaviors (fields and methods) from another class.
This allows the creation of a hierarchy where child (subclass) objects can reuse, extend, or customize code from their parent (superclass).
π Formal Definition
Inheritance is a mechanism that allows one class to inherit the fields and methods of another class, enabling code reuse, polymorphism, and hierarchical relationships.
π§± Key Characteristics of Inheritance
Feature | Description |
---|---|
Code Reusability | Share common functionality in parent class |
Extensibility | Add or modify behavior in the child class |
Hierarchy Support | Organize classes in logical structures |
Polymorphism Base | Enables method overriding and dynamic behavior |
Single Source of Truth | Avoid duplicate code in multiple classes |
π οΈ Basic Java Example
// Superclass
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
// Subclass
class Dog extends Animal {
void bark() {
System.out.println("Dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // inherited
dog.bark(); // subclass-specific
}
}
πͺ Real-Life Analogy
Inheritance in Real Life:
- A child inherits traits like eye color or blood type from parents.
- In software, a subclass inherits methods and properties from its superclass.
π Types of Inheritance in Java
Type | Description | Supported in Java |
---|---|---|
Single | One subclass inherits one superclass | β Yes |
Multilevel | Class inherits a class that also inherited another class | β Yes |
Hierarchical | Multiple classes inherit from a single superclass | β Yes |
Multiple | One class inherits from multiple classes | β (use Interfaces) |
Hybrid | Combination of multiple types (handled using interfaces in Java) | β With Interfaces |
π Java Syntax
class Parent {
void show() {
System.out.println("Parent method");
}
}
class Child extends Parent {
void display() {
System.out.println("Child method");
}
}
Use the extends
keyword to establish inheritance.
π Method Overriding
class Animal {
void sound() {
System.out.println("Generic Animal Sound");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Meow");
}
}
Allows runtime polymorphism.
π§ Key Interview Points
- Inheritance promotes reusability and reduces redundancy.
- Constructors are not inherited, but
super()
can call parent constructor. - Private members of a class are not inherited directly.
- Java does not support multiple inheritance with classes (to avoid ambiguity like the Diamond Problem).
- Java allows multiple inheritance using interfaces.
βοΈ Inheritance vs Composition vs Interface
Feature | Inheritance | Composition | Interface |
---|---|---|---|
Code Reuse | β Yes (via parent class) | β Yes (via object references) | β Yes (via contract enforcement) |
Flexibility | β Rigid | β High | β High |
Tight Coupling | β Tight | β Loose | β Loose |
Multiple Support | β No (classes) | β Yes | β Yes |
Use When | IS-A relationship | HAS-A relationship | Need for abstraction across classes |
β Best Practices
- Favor composition over inheritance when behavior is not strictly hierarchical.
- Keep your class hierarchies shallow to maintain simplicity.
- Avoid deep inheritance trees β they are hard to maintain and test.
- Use
final
to prevent inheritance when necessary. - Use interfaces when multiple behavior contracts are needed.
β Interview Questions with Answers
Q1. What is inheritance in OOP? A: A mechanism by which a subclass acquires the properties and behavior of a superclass.
Q2. Can private fields be inherited? A: No, private members are not accessible in the subclass, but they exist in memory.
Q3. What is method overriding? A: Redefining a superclass method in a subclass with the same signature.
Q4. Why Java doesnβt support multiple inheritance with classes? A: To avoid ambiguity (Diamond Problem). Interfaces solve this limitation.
Q5. What's the difference between super()
and this()
? A: super()
calls the parent constructor; this()
calls another constructor in the same class.
π Common Use Cases of Inheritance
Use Case | Example |
---|---|
Shared Functionality | Vehicle β Car , Bike |
Reusable Logic | User β Admin , Guest |
Behavioral Polymorphism | Shape β Circle , Rectangle |
Framework Design | Spring, Hibernate use inheritance |
π Summary
- Inheritance is key to code reusability and object hierarchy.
- It forms the backbone of polymorphism and modular class design.
- Use it wisely with abstract classes or interfaces, and prefer shallow, meaningful class hierarchies.