OOP Interview Question

What is inheritance in OOP?

Updated 2026-08-16 · Beginner friendly
Quick answer

Inheritance is an OOP feature where a new class reuses the fields and methods of an existing class. The existing class is called the parent or base class, and the new one is called the child or derived class. It promotes code reuse and lets you build a clear hierarchy of related types.

Key takeaways
  • Inheritance lets a child class reuse the fields and methods of a parent class.
  • It models an is a relationship and removes duplicate code across related types.
  • Java allows multiple inheritance only through interfaces to avoid the diamond problem.

The core idea

Inheritance lets a child class automatically get everything a parent class already has, and then add or change things. Instead of copying code, you extend it. A Dog is an Animal, so Dog can inherit eat and sleep from Animal and simply add bark.

class Animal {
public:
    void eat() { cout << "eating"; }
    void sleep() { cout << "sleeping"; }
};

class Dog : public Animal {   // Dog inherits from Animal
public:
    void bark() { cout << "woof"; }
};

Dog d;
d.eat();    // inherited
d.bark();   // its own
class Animal {
    void eat() { System.out.println("eating"); }
    void sleep() { System.out.println("sleeping"); }
}

class Dog extends Animal {   // Dog inherits from Animal
    void bark() { System.out.println("woof"); }
}

Dog d = new Dog();
d.eat();    // inherited
d.bark();   // its own
class Animal:
    def eat(self):
        print("eating")
    def sleep(self):
        print("sleeping")

class Dog(Animal):            # Dog inherits from Animal
    def bark(self):
        print("woof")

d = Dog()
d.eat()    # inherited
d.bark()   # its own

Common types of inheritance

Why it helps

Inheritance removes duplicate code and creates a natural is a relationship. When shared logic lives in the parent, a fix or improvement there instantly benefits every child class.

In the interview

Be ready for the follow up on why Java does not support multiple inheritance with classes. The short answer is the diamond problem, where a class could inherit two versions of the same method and the compiler would not know which to use. Java solves this by allowing multiple inheritance only through interfaces.

Frequently asked questions

Why does Java not support multiple inheritance with classes?

To avoid the diamond problem, where a class could inherit two versions of the same method and the compiler would not know which to use. Interfaces avoid this.

What is the difference between inheritance and composition?

Inheritance is an is a relationship where a child extends a parent, while composition is a has a relationship where a class holds another as a field.

Want the full OOP guide?

Read every OOP concept with notes, diagrams, and code in one place. Track your progress as you go.

Open the OOP guide All OOP questions