# Overview of Inheritance

> Inheritance is an object-oriented programming mechanism that lets a derived class (subclass) acquire the fields and methods of an existing base class (superclass) instead of rewriting that logic. The derived class can add its own members or override base methods, so common logic lives in one place and the compiler enforces is-a relationships.

Canonical: https://devsunite.com/resources/core-subjects/oop/overview-of-inheritance
Last updated: 2026-11-22

Subject: OOP · Category: Inheritance · 2 min read

Inheritance is one of the four pillars of object-oriented programming, alongside encapsulation, abstraction, and polymorphism. It lets a new class, called the derived class or subclass, reuse the fields and methods of an existing class, called the base class or superclass, instead of rewriting that logic from scratch.

## How it works

When a class inherits from another, it automatically gets access to the base class's public and protected members. The derived class can then add its own new members, or override the base class's methods to change their behavior, without touching the base class's own code at all.

This is the mechanism that turns "a Car is a Vehicle" from a comment in your head into something the compiler actually understands and enforces.

## A worked example

```cpp
#include <iostream>
using namespace std;

class Vehicle {
public:
    string brand;
    void honk() {
        cout << brand << " says beep!" << endl;
    }
};

class Car : public Vehicle {
public:
    int numDoors;
};

int main() {
    Car myCar;
    myCar.brand = "Toyota";   // inherited field from Vehicle
    myCar.numDoors = 4;       // Car's own field
    myCar.honk();             // inherited method from Vehicle
    return 0;
}
```

Running this prints `Toyota says beep!`. `Car` never defined `brand` or `honk()` itself, it got both for free from `Vehicle` simply by inheriting from it, and then added `numDoors` as something only `Car` has.

## Where this matters in real systems

Nearly every GUI framework leans on this heavily. In Java's Swing or Android's View system, every button, text field, and layout ultimately inherits from a common base class (`View` on Android, `Component` in Swing) that provides shared behavior like handling clicks, drawing, and layout measurement. Each specific widget only needs to add what makes it different, a button adds "look pressed and fire a click event," a text field adds "accept keyboard input," and both get the shared plumbing for free.

## What interviewers actually ask about inheritance

- **"What problem does inheritance actually solve?"** Code reuse and modeling real "is-a" relationships, so shared behavior lives in one place instead of being duplicated.
- **"What's the difference between inheritance and composition?"** Inheritance models "is-a" (a Car is a Vehicle); composition models "has-a" (a Car has an Engine). Composition is generally preferred when the relationship isn't a true specialization.
- **"Can a derived class access private members of the base class?"** No, private members are never inherited in the sense of being directly accessible, only public and protected members are.
- **"What happens if you don't call the base class constructor explicitly?"** The base class's default constructor runs automatically before the derived class's constructor body executes.

## Key takeaways

- Inheritance lets a derived class reuse a base class's fields and methods.
- It models real "is-a" relationships between types.
- Private members of the base class are not directly accessible from the derived class.
- It's one of OOP's four core pillars, alongside encapsulation, abstraction, and polymorphism.

## Related

- [Next: Super and Sub Classes](https://devsunite.com/resources/core-subjects/oop/super-and-sub-classes)
- [More in Inheritance: Types of Inheritance](https://devsunite.com/resources/core-subjects/oop/types-of-inheritance)
- [More in Inheritance: Access Specifiers in Inheritance](https://devsunite.com/resources/core-subjects/oop/access-specifiers-in-inheritance)
- [More in Inheritance: Constructor and Destructor Order in Inheritance](https://devsunite.com/resources/core-subjects/oop/constructor-and-destructor-in-inheritance)
- [More in Inheritance: Virtual Base Classes](https://devsunite.com/resources/core-subjects/oop/virtual-base-classes)
- [More in Inheritance: Composition vs Aggregation vs Inheritance](https://devsunite.com/resources/core-subjects/oop/composition-vs-aggregation-vs-inheritance)
- [All 72 OOP interview questions](https://devsunite.com/resources/core-subjects/oop)
