In this tutorial, you’ll learn Inheritance in Java — one of the core Object-Oriented Programming (OOP) concepts. We’ll also go through real-world scenarios, interview questions, and coding examples that help you master it from both conceptual and practical angles.
1. What is Inheritance?
Inheritance allows one class (child/subclass) to acquire the properties and behavior (fields and methods) of another class (parent/superclass). It helps in code reusability and avoids duplication.
Definition: Inheritance represents an "is-a" relationship between two classes.
class Animal {
void eat() {
System.out.println("I can eat");
}
}
class Dog extends Animal {
void bark() {
System.out.println("I can bark");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // inherited from Animal
d.bark(); // defined in Dog
}
}
Output:
I can eat
I can bark
2. When to Use Inheritance
- Use it when classes share common behavior or properties.
- There’s a logical is-a relationship — e.g., Dog is an Animal, Car is a Vehicle.
- Avoid it if classes are only related by usage — use composition (has-a) instead.
3. Why Use Inheritance (Benefits)
- Code Reusability – Common code resides in the parent.
- Extensibility – Add new subclasses easily.
- Polymorphism Support – Enables runtime method overriding.
- Less Duplication – Reduces repetitive logic.
Animal a = new Dog();
a.eat(); // Calls Dog’s version if overridden
4. Types of Inheritance in Java
- Single Inheritance: One class inherits another.
- Multilevel Inheritance: Chain of inheritance.
- Hierarchical Inheritance: Multiple subclasses inherit one parent.
- Multiple Inheritance: Not allowed with classes (to avoid ambiguity), but supported via interfaces.
5. Scenario-Based Interview Questions & Answers
Q1. Why would you use inheritance in a real-world project?
Scenario: In an employee management system, all employees share common fields like id, name, and salary, while managers have additional fields like bonus.
class Employee {
int id;
String name;
double salary;
}
class Manager extends Employee {
double bonus;
}
Answer: This reduces duplicate code and allows adding new employee types (like Developer or HR) easily.
Q2. Inheritance vs Composition?
// Inheritance (is-a)
class Car extends Vehicle { }
// Composition (has-a)
class Car {
Engine engine = new Engine();
}
Use inheritance for behavior sharing, and composition for using other class features.
Q3. What happens when parent and child have same method?
class Animal {
void eat() { System.out.println("Animal eats"); }
}
class Dog extends Animal {
@Override
void eat() { System.out.println("Dog eats bones"); }
}
Output: Dog eats bones ✅ Method overriding (runtime polymorphism).
Q4. Can a child access private parent members?
❌ No, private members are not inherited.
Use protected for limited access.
Q5. Why static method call gives "Parent static"?
class Parent {
static void display() { System.out.println("Parent static"); }
}
class Child extends Parent {
static void display() { System.out.println("Child static"); }
}
public class Main {
public static void main(String[] args) {
Parent p = new Child();
p.display();
}
}
Output: Parent static
Explanation: Static methods are class-level and resolved at compile time, not runtime. Hence, the method in the reference type (Parent) is used — this is method hiding, not overriding.
6. Common Coding Questions on Inheritance
Q1. Demonstrate Method Overriding
class Vehicle {
void start() { System.out.println("Vehicle starts"); }
}
class Car extends Vehicle {
@Override
void start() { System.out.println("Car starts with key"); }
}
Output: Car starts with key
Q2. Banking Example
class Account {
double balance = 1000;
void withdraw(double amount) {
balance -= amount;
System.out.println("Withdrawn: " + amount);
}
}
class SavingsAccount extends Account {
@Override
void withdraw(double amount) {
if (balance - amount < 500)
System.out.println("Minimum balance limit reached!");
else
super.withdraw(amount);
}
}
Q3. Multi-Level Inheritance
class Person { void showPerson() { System.out.println("I am a person"); } }
class Employee extends Person { void showEmployee() { System.out.println("I am an employee"); } }
class Manager extends Employee { void showManager() { System.out.println("I am a manager"); } }
Output:
I am a person
I am an employee
I am a manager
Q4. Constructor Call Order
class A { A() { System.out.println("A constructor"); } }
class B extends A { B() { System.out.println("B constructor"); } }
class C extends B { C() { System.out.println("C constructor"); } }
public class Main {
public static void main(String[] args) {
new C();
}
}
Output:
A constructor
B constructor
C constructor
Parent constructors always execute first.
Q5. Can a child override a final method?
❌ No. Final methods cannot be overridden.
Q6. Calling Parent Method using super
class Animal {
void sound() { System.out.println("Animal makes sound"); }
}
class Dog extends Animal {
@Override
void sound() {
super.sound();
System.out.println("Dog barks");
}
}
Output:
Animal makes sound
Dog barks
Q7. Parent Reference to Child Object
Parent p = new Child(); // upcasting
p.show(); // works
((Child)p).play(); // downcasting
Q8. Real-World Use Case (Notifications)
abstract class Notification {
String message;
abstract void send();
}
class EmailNotification extends Notification {
void send() { System.out.println("Sending Email: " + message); }
}
class SMSNotification extends Notification {
void send() { System.out.println("Sending SMS: " + message); }
}
Usage:
List<Notification> list = List.of(new EmailNotification(), new SMSNotification());
for (Notification n : list) n.send();
✅ Demonstrates polymorphism through inheritance.
Q9. Multiple Inheritance with Interfaces
interface A { void show(); }
interface B { void display(); }
class C implements A, B {
public void show() { System.out.println("A's show"); }
public void display() { System.out.println("B's display"); }
}
✅ Achieves multiple inheritance of type without ambiguity.
7. Conclusion
Inheritance in Java is a powerful OOP concept that promotes clean, maintainable, and reusable code. It’s the foundation for polymorphism, abstraction, and framework-level architecture (like Spring’s class hierarchies).
Key Takeaways:
- Use inheritance only when there is a true is-a relationship.
- Prefer composition when classes just use other functionalities.
- Understand method overriding, constructor chaining, and static method hiding well — these are common interview traps.
That’s it! 🎉 You’ve learned how inheritance works in Java with clear real-world scenarios, interview-style questions, and hands-on examples.
Next Step: Explore Polymorphism and Abstract Classes in Java to understand how inheritance powers dynamic behavior in enterprise-level applications.
0 Comments