OOP Concepts Overview
OOP is usually explained through four main pillars:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
You will study each deeply later. For now, learn the clean meaning of each one.
1. Encapsulation
Encapsulation means keeping data and related functions together inside a class, while controlling access to the data.
Example:
class BankAccount {
private:
double balance;
public:
void deposit(double amount);
bool withdraw(double amount);
};
The balance is private. Outside code must use safe public functions.
One-Line Viva Answer
Encapsulation binds data and methods together and protects internal state from direct outside access.
2. Inheritance
Inheritance means creating a new class from an existing class.
It models an is-a relationship.
Example:
class Animal {};
class Dog : public Animal {};
A Dog is an Animal, so inheritance makes sense.
One-Line Viva Answer
Inheritance allows a derived class to reuse and extend a base class when there is a true is-a relationship.
3. Polymorphism
Polymorphism means "many forms."
In C++, it commonly means the same function call can behave differently depending on the actual object type.
Example idea:
animal->speak();
If animal points to a Dog, it may bark. If it points to a Cat, it may meow.
One-Line Viva Answer
Polymorphism allows one interface or function call to produce different behavior for different object types.
4. Abstraction
Abstraction means showing only essential details and hiding implementation complexity.
Example:
class PaymentProcessor {
public:
virtual void pay(double amount) = 0;
};
The user knows they can call pay(). They do not need to know whether payment happens by card, mobile banking, or another method.
One-Line Viva Answer
Abstraction hides implementation details and exposes only the necessary interface.
How They Work Together
| Pillar | Main Question |
|---|---|
| Encapsulation | How do I protect object state? |
| Inheritance | Is this a true is-a relationship? |
| Polymorphism | Can different objects respond through one interface? |
| Abstraction | What details can I hide behind a simple interface? |
C++ Tools for These Pillars
| Pillar | C++ tools |
|---|---|
| Encapsulation | class, private, public, getters/setters with validation |
| Inheritance | class Derived : public Base |
| Polymorphism | function overloading, operator overloading, virtual functions |
| Abstraction | abstract classes, pure virtual functions, interfaces |
Important Distinction
Encapsulation and abstraction are related, but not the same.
- Encapsulation protects internal state.
- Abstraction hides unnecessary details.
Example:
- A
BankAccounthidingbalanceis encapsulation. - A
PaymentProcessorhiding card/mobile-bank implementation is abstraction.
Viva Answer
The four pillars of OOP are encapsulation, inheritance, polymorphism, and abstraction. Encapsulation protects state, inheritance models is-a relationships, polymorphism allows many forms of behavior through one interface, and abstraction hides implementation details behind a simple interface.
Quick Check
- Which pillar protects data?
- Which pillar uses
is-a? - Which pillar uses virtual functions often?
- Which pillar hides complex implementation details?