What Is Inheritance?
Inheritance is a way to create a new class by building on top of an existing class.
The new class automatically gets the attributes and methods of the existing class and can add more or change some behaviours.
Key terms:
Base class (or parent class): the existing class that provides common features.
Derived class (or child class): the new class that reuses the base class and extends or specialises it.
Example in real life:
“A car is a vehicle.”
“A savings account is a bank account.”
“A premium user is a user.”
Inheritance models these “is‑a” relationships in code.
In Low Level Design, inheritance helps you avoid repeating common fields and methods in multiple related classes and organise your types into clear hierarchies.
Why Use Inheritance?
Inheritance is useful when:
Several types share common data and behaviour, and you want to define that once.
Some types are special cases of a more general concept.
Benefits:
Code reuse
Common logic lives in the base class, used by all derived classes.
Clear hierarchy
You can talk about general types and specific types (e.g.,
VehiclevsCarandBike).
Polymorphism support
You can write functions that work with the base type and accept any derived type.
However, inheritance should be used when there is a real “is‑a” relationship, not just for convenience.
Basic Inheritance Syntax in C++
To derive a class from another in C++, you use : the with an access specifier:
Baseis the base class.Derivedis the derived class.publicInheritance is the most common: it means aDerivedis aBasefrom the outside.
If you say:
then:
dhas all public (and protected) members ofBase, plus anything defined inDerived.You can use
dwherever anBaseis expected (via references or pointers).
Example: Vehicle and Specific Vehicle Types
Consider a simple hierarchy to represent vehicles in a transport or parking system.
Base Class: Vehicle
This class captures data and behavior common to all vehicles.
Derived Classes: Car and Bike
Usage:
Here:
CarandBikereuse common vehicle fields (registrationNumber,brand) fromVehicle.They add their own specific data and methods.
This is inheritance modeling: “Car is a Vehicle” and “Bike is a Vehicle”.
Inheritance and Polymorphism (Overview)
One of the most powerful uses of inheritance is polymorphism: being able to use a base class reference or pointer to work with objects of different derived classes.
Extend the Vehicle example with a virtual function:
Derived classes override:
Now:
Even though printVehicle takes a Vehicle&, it calls the correct printDetails based on the actual object type.
This is inheritance enabling polymorphism.
When to Use Inheritance vs Composition
Inheritance is not the only way to reuse code. You should compare it with the composition:
Inheritance models an “is‑a” relationship.
A
Caris aVehicle.A
SavingsAccountis aBankAccount.
Composition models a “has‑a” relationship.
A
Carhas anEngine.A
Userhas anAddress.
Use inheritance when:
The derived type is truly a specialized form of the base.
The base class defines a meaningful interface that all derived classes logically share.
Use composition when:
You just want to reuse behavior, but there is no clear “is‑a” relationship.
You are adding capabilities rather than types. For example, a
Reportthat has anPrinterobject.
In LLD interviews, it is often appreciated if you say:
“I will use inheritance here because this is an ‘is‑a’ relationship.”
Or “Here, composition is better; this is more of a ‘has‑a’ relationship.”
Access Modifiers in Inheritance
In C++, you caninherit fromh public, protected, or private. For most LLD and design examples, you will use public inheritance.
Public members of
Baseremain public inDerived.Protected members remain protected.
Private members
Baseare not directly accessible inDerived, but they exist as part of the object.
protected members in the base are visible inside derived classes but not outside the hierarchy.
For LLD teaching and interviews, focusing on public inheritance with clear “is‑a” relationships is usually enough.
Common Inheritance Pitfalls
Some common issues beginners run into:
Using inheritance just for code reuse
If two classes share code but are not logically “is‑a”, prefer composition.
Making base classes too general and vague
A base like
ManagerorHandlerwAnunclear responsibility can make the hierarchy confusing.
Deep inheritance hierarchies
Many levels of inheritance can make behavior hard to follow.
Keep hierarchies as shallow and meaningful as possible.
Forgetting virtual destructors
If you delete derived objects through a base pointer without a virtual destructor, behavior is undefined.
Being aware of these helps you design safer, more maintainable hierarchies.
Summary
Inheritance lets you define a new class based on an existing one, reusing and specializing its data and behavior.
It models “is‑a” relationships like Car is a Vehicle or SavingsAccount is a BankAccount.
You learned:
The meaning of base and derived classes and the “is‑a” relationship.
Basic inheritance syntax in C++ with
: public Base.How to share common fields and behavior in a base class and extend them in derived classes.
How inheritance supports polymorphism when combined with virtual functions.
When to prefer inheritance vs composition and common pitfalls to avoid.