Four Pillars of Object-
Oriented Programming
(OOP)
1. Data Abstraction
Definition: Hides implementation details while exposing
only the essential features of an object.
Benefits: Reduces code complexity and focuses on relevant
functionality.
Example:
JavaScript:
class Car {
#engine; // private field
constructor() {
this.#engine = 'V8';
}
startEngine() {
console.log('Engine started');
Four Pillars of Object-Oriented Programming (OOP) 1
}
getEngine() {
return this.#engine;
}
}
const car = new Car();
console.log(car.getEngine()); // Output: 'V8'
TypeScript:
class Car {
private engine: string = 'V8';
startEngine() {
console.log('Engine started');
}
getEngine() {
return this.engine;
}
}
const car = new Car();
console.log(car.getEngine()); // Output: 'V8'
C++:
class Car {
private:
std::string engine = "V8";
public:
void startEngine() {
std::cout << "Engine started" << std::endl;
}
std::string getEngine() {
Four Pillars of Object-Oriented Programming (OOP) 2
return engine;
}
};
int main() {
Car car;
std::cout << car.getEngine() << std::endl; // Output: "V8"
car.startEngine();
return 0;
}
Python:
class Car:
def __init__(self):
self.__engine = "V8"
def start_engine(self):
print("Engine started")
def get_engine(self):
return self.__engine
car = Car()
print(car.get_engine()) # Output: "V8"
2. Inheritance
Definition: A mechanism where a new class is derived from
an existing class.
Types of Inheritance:
Single Inheritance: One class inherits from another.
Multilevel Inheritance: A class inherits from another
class, which in turn inherits from another.
Multiple Inheritance: A class inherits from multiple
classes (not recommended in some languages).
Four Pillars of Object-Oriented Programming (OOP) 3
Hierarchical Inheritance: Multiple classes inherit from
a single base class.
Hybrid Inheritance: Combination of multiple types of
inheritance.
Examples:
JavaScript:
class Animal {
speak() {
console.log("Animal speaks");
}
}
class Dog extends Animal {
speak() {
console.log("Dog barks");
}
}
const dog = new Dog();
dog.speak(); // Output: "Dog barks"
TypeScript:
class Animal {
speak() {
console.log("Animal speaks");
}
}
class Dog extends Animal {
speak() {
console.log("Dog barks");
}
}
Four Pillars of Object-Oriented Programming (OOP) 4
const dog = new Dog();
dog.speak(); // Output: "Dog barks"
C++:
class Animal {
public:
void speak() {
std::cout << "Animal speaks" << std::endl;
}
};
class Dog : public Animal {
public:
void speak() {
std::cout << "Dog barks" << std::endl;
}
};
int main() {
Dog dog;
dog.speak(); // Output: "Dog barks"
return 0;
}
Python:
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
print("Dog barks")
dog = Dog()
dog.speak() # Output: "Dog barks"
Four Pillars of Object-Oriented Programming (OOP) 5
3. Polymorphism
Definition: The ability for objects to take many forms,
allowing the same method or function to behave differently
based on the object.
Types:
Compile-time Polymorphism: Achieved via method
overloading (function with the same name but different
parameters).
Run-time Polymorphism: Achieved via method overriding
(derived class changes the behavior of a base class
method).
Examples:
JavaScript (Run-time):
class Animal {
speak() {
console.log("Animal speaks");
}
}
class Dog extends Animal {
speak() {
console.log("Dog barks");
}
}
let animal = new Animal();
let dog = new Dog();
animal.speak(); // Output: "Animal speaks"
dog.speak(); // Output: "Dog barks"
TypeScript (Run-time):
class Animal {
speak() {
console.log("Animal speaks");
Four Pillars of Object-Oriented Programming (OOP) 6
}
}
class Dog extends Animal {
speak() {
console.log("Dog barks");
}
}
const animal = new Animal();
const dog = new Dog();
animal.speak(); // Output: "Animal speaks"
dog.speak(); // Output: "Dog barks"
C++ (Run-time):
class Animal {
public:
virtual void speak() {
std::cout << "Animal speaks" << std::endl;
}
};
class Dog : public Animal {
public:
void speak() override {
std::cout << "Dog barks" << std::endl;
}
};
int main() {
Animal* animal = new Animal();
Animal* dog = new Dog();
animal->speak(); // Output: "Animal speaks"
dog->speak(); // Output: "Dog barks"
delete animal;
delete dog;
Four Pillars of Object-Oriented Programming (OOP) 7
return 0;
}
Python (Run-time):
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
print("Dog barks")
animal = Animal()
dog = Dog()
animal.speak() # Output: "Animal speaks"
dog.speak() # Output: "Dog barks"
4. Encapsulation
Definition: The practice of bundling data and methods that
operate on that data within a single unit (class), and
restricting access to some of the object’s components.
Example:
JavaScript:
class Account {
#balance = 0; // private field
deposit(amount) {
this.#balance += amount;
}
getBalance() {
return this.#balance;
}
}
Four Pillars of Object-Oriented Programming (OOP) 8
const account = new Account();
account.deposit(100);
console.log(account.getBalance()); // Output: 100
TypeScript:
class Account {
private balance: number = 0;
deposit(amount: number) {
this.balance += amount;
}
getBalance() {
return this.balance;
}
}
const account = new Account();
account.deposit(100);
console.log(account.getBalance()); // Output: 100
C++:
class Account {
private:
double balance = 0.0;
public:
void deposit(double amount) {
balance += amount;
}
double getBalance() {
return balance;
}
};
Four Pillars of Object-Oriented Programming (OOP) 9
int main() {
Account account;
account.deposit(100);
std::cout << account.getBalance() << std::endl; // Output: 100
return 0;
}
Python:
class Account:
def __init__(self):
self.__balance = 0
def deposit(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
account = Account()
account.deposit(100)
print(account.get_balance()) # Output: 100
Access Specifiers (C++)
Public: Accessible from outside the class.
Private: Not accessible from outside the class.
Protected: Accessible in the class and its derived classes.
Virtual Functions (C++)
Definition: Virtual functions enable runtime polymorphism.
A virtual function in a base class can be overridden by a
derived class to provide custom behavior.
Example:
Four Pillars of Object-Oriented Programming (OOP) 10
class Animal {
public:
virtual void speak() {
std::cout << "Animal speaks" << std::endl;
}
};
class Dog : public Animal {
public:
void speak() override {
std::cout << "Dog barks" << std::endl;
}
};
Pure Object-Oriented Programming Languages
Smalltalk
Eiffel
Self
Operator Overloading
Definition: Allows custom behavior for operators (e.g., +,
-, *, etc.) with user-defined data types.
Example (C++):
class Complex {
public:
int real, imag;
Complex operator + (const Complex& c) {
Complex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return temp;
}
};
Four Pillars of Object-Oriented Programming (OOP) 11
Summary of Object-Oriented Programming (OOP)
Concepts
Concept Definition Examples
Data
Abstraction
Hides implementation
details and exposes
only essential
features.
JavaScript: #engine field in
Car class. TypeScript:
private engine in Car class.
C++: private engine in Car
class. Python: __engine in
Car class.
Inheritance
Deriving a new class
from an existing class
for reusability.
Single, Multi-level,
Multiple, Hierarchical,
Hybrid inheritance. Example:
Dog inherits Animal .
Polymorphism
The ability for objects
to take many forms.
Run-time: Methods can be
overridden. JavaScript: Dog
class overrides speak() from
Animal class.
Encapsulation
Bundling data and
methods, restricting
access to certain
components.
JavaScript: #balance in
Account . TypeScript: private
balance in Account . C++:
private balance in Account .
Python: __balance in Account .
Access
Specifiers
(C++)
Defines the access
level for class
members.
Public: Accessible outside
class. Private: Not
accessible outside class.
Protected: Accessible in
derived classes.
Virtual
Functions
(C++)
Enables run-time
polymorphism, allowing
derived classes to
override base class
functions.
virtual void speak() in Animal
class; overridden in Dog
class.
Pure OOP
Languages
Languages that strictly
follow OOP principles.
Smalltalk, Eiffel, Self
Four Pillars of Object-Oriented Programming (OOP) 12
Operator
Overloading
Allows operators (e.g.,
+ , - ) to be redefined
for user-defined types.
C++: operator + for Complex
class to add two complex
numbers.
Types of
Constructors
Special methods for
object initialization.
Default Constructor: No
arguments. Parameterized
Constructor: Initializes
with specific values. Copy
Constructor: Copies state
from another object.
Polymorphism
Types
Compile-time: Achieved
through overloading.
Run-time: Achieved
through overriding.
Compile-time: Method
overloading. Run-time:
Method overriding.
Four Pillars of Object-Oriented Programming (OOP) 13

Mastering OOP: Understanding the Four Core Pillars

  • 1.
    Four Pillars ofObject- Oriented Programming (OOP) 1. Data Abstraction Definition: Hides implementation details while exposing only the essential features of an object. Benefits: Reduces code complexity and focuses on relevant functionality. Example: JavaScript: class Car { #engine; // private field constructor() { this.#engine = 'V8'; } startEngine() { console.log('Engine started'); Four Pillars of Object-Oriented Programming (OOP) 1
  • 2.
    } getEngine() { return this.#engine; } } constcar = new Car(); console.log(car.getEngine()); // Output: 'V8' TypeScript: class Car { private engine: string = 'V8'; startEngine() { console.log('Engine started'); } getEngine() { return this.engine; } } const car = new Car(); console.log(car.getEngine()); // Output: 'V8' C++: class Car { private: std::string engine = "V8"; public: void startEngine() { std::cout << "Engine started" << std::endl; } std::string getEngine() { Four Pillars of Object-Oriented Programming (OOP) 2
  • 3.
    return engine; } }; int main(){ Car car; std::cout << car.getEngine() << std::endl; // Output: "V8" car.startEngine(); return 0; } Python: class Car: def __init__(self): self.__engine = "V8" def start_engine(self): print("Engine started") def get_engine(self): return self.__engine car = Car() print(car.get_engine()) # Output: "V8" 2. Inheritance Definition: A mechanism where a new class is derived from an existing class. Types of Inheritance: Single Inheritance: One class inherits from another. Multilevel Inheritance: A class inherits from another class, which in turn inherits from another. Multiple Inheritance: A class inherits from multiple classes (not recommended in some languages). Four Pillars of Object-Oriented Programming (OOP) 3
  • 4.
    Hierarchical Inheritance: Multipleclasses inherit from a single base class. Hybrid Inheritance: Combination of multiple types of inheritance. Examples: JavaScript: class Animal { speak() { console.log("Animal speaks"); } } class Dog extends Animal { speak() { console.log("Dog barks"); } } const dog = new Dog(); dog.speak(); // Output: "Dog barks" TypeScript: class Animal { speak() { console.log("Animal speaks"); } } class Dog extends Animal { speak() { console.log("Dog barks"); } } Four Pillars of Object-Oriented Programming (OOP) 4
  • 5.
    const dog =new Dog(); dog.speak(); // Output: "Dog barks" C++: class Animal { public: void speak() { std::cout << "Animal speaks" << std::endl; } }; class Dog : public Animal { public: void speak() { std::cout << "Dog barks" << std::endl; } }; int main() { Dog dog; dog.speak(); // Output: "Dog barks" return 0; } Python: class Animal: def speak(self): print("Animal speaks") class Dog(Animal): def speak(self): print("Dog barks") dog = Dog() dog.speak() # Output: "Dog barks" Four Pillars of Object-Oriented Programming (OOP) 5
  • 6.
    3. Polymorphism Definition: Theability for objects to take many forms, allowing the same method or function to behave differently based on the object. Types: Compile-time Polymorphism: Achieved via method overloading (function with the same name but different parameters). Run-time Polymorphism: Achieved via method overriding (derived class changes the behavior of a base class method). Examples: JavaScript (Run-time): class Animal { speak() { console.log("Animal speaks"); } } class Dog extends Animal { speak() { console.log("Dog barks"); } } let animal = new Animal(); let dog = new Dog(); animal.speak(); // Output: "Animal speaks" dog.speak(); // Output: "Dog barks" TypeScript (Run-time): class Animal { speak() { console.log("Animal speaks"); Four Pillars of Object-Oriented Programming (OOP) 6
  • 7.
    } } class Dog extendsAnimal { speak() { console.log("Dog barks"); } } const animal = new Animal(); const dog = new Dog(); animal.speak(); // Output: "Animal speaks" dog.speak(); // Output: "Dog barks" C++ (Run-time): class Animal { public: virtual void speak() { std::cout << "Animal speaks" << std::endl; } }; class Dog : public Animal { public: void speak() override { std::cout << "Dog barks" << std::endl; } }; int main() { Animal* animal = new Animal(); Animal* dog = new Dog(); animal->speak(); // Output: "Animal speaks" dog->speak(); // Output: "Dog barks" delete animal; delete dog; Four Pillars of Object-Oriented Programming (OOP) 7
  • 8.
    return 0; } Python (Run-time): classAnimal: def speak(self): print("Animal speaks") class Dog(Animal): def speak(self): print("Dog barks") animal = Animal() dog = Dog() animal.speak() # Output: "Animal speaks" dog.speak() # Output: "Dog barks" 4. Encapsulation Definition: The practice of bundling data and methods that operate on that data within a single unit (class), and restricting access to some of the object’s components. Example: JavaScript: class Account { #balance = 0; // private field deposit(amount) { this.#balance += amount; } getBalance() { return this.#balance; } } Four Pillars of Object-Oriented Programming (OOP) 8
  • 9.
    const account =new Account(); account.deposit(100); console.log(account.getBalance()); // Output: 100 TypeScript: class Account { private balance: number = 0; deposit(amount: number) { this.balance += amount; } getBalance() { return this.balance; } } const account = new Account(); account.deposit(100); console.log(account.getBalance()); // Output: 100 C++: class Account { private: double balance = 0.0; public: void deposit(double amount) { balance += amount; } double getBalance() { return balance; } }; Four Pillars of Object-Oriented Programming (OOP) 9
  • 10.
    int main() { Accountaccount; account.deposit(100); std::cout << account.getBalance() << std::endl; // Output: 100 return 0; } Python: class Account: def __init__(self): self.__balance = 0 def deposit(self, amount): self.__balance += amount def get_balance(self): return self.__balance account = Account() account.deposit(100) print(account.get_balance()) # Output: 100 Access Specifiers (C++) Public: Accessible from outside the class. Private: Not accessible from outside the class. Protected: Accessible in the class and its derived classes. Virtual Functions (C++) Definition: Virtual functions enable runtime polymorphism. A virtual function in a base class can be overridden by a derived class to provide custom behavior. Example: Four Pillars of Object-Oriented Programming (OOP) 10
  • 11.
    class Animal { public: virtualvoid speak() { std::cout << "Animal speaks" << std::endl; } }; class Dog : public Animal { public: void speak() override { std::cout << "Dog barks" << std::endl; } }; Pure Object-Oriented Programming Languages Smalltalk Eiffel Self Operator Overloading Definition: Allows custom behavior for operators (e.g., +, -, *, etc.) with user-defined data types. Example (C++): class Complex { public: int real, imag; Complex operator + (const Complex& c) { Complex temp; temp.real = real + c.real; temp.imag = imag + c.imag; return temp; } }; Four Pillars of Object-Oriented Programming (OOP) 11
  • 12.
    Summary of Object-OrientedProgramming (OOP) Concepts Concept Definition Examples Data Abstraction Hides implementation details and exposes only essential features. JavaScript: #engine field in Car class. TypeScript: private engine in Car class. C++: private engine in Car class. Python: __engine in Car class. Inheritance Deriving a new class from an existing class for reusability. Single, Multi-level, Multiple, Hierarchical, Hybrid inheritance. Example: Dog inherits Animal . Polymorphism The ability for objects to take many forms. Run-time: Methods can be overridden. JavaScript: Dog class overrides speak() from Animal class. Encapsulation Bundling data and methods, restricting access to certain components. JavaScript: #balance in Account . TypeScript: private balance in Account . C++: private balance in Account . Python: __balance in Account . Access Specifiers (C++) Defines the access level for class members. Public: Accessible outside class. Private: Not accessible outside class. Protected: Accessible in derived classes. Virtual Functions (C++) Enables run-time polymorphism, allowing derived classes to override base class functions. virtual void speak() in Animal class; overridden in Dog class. Pure OOP Languages Languages that strictly follow OOP principles. Smalltalk, Eiffel, Self Four Pillars of Object-Oriented Programming (OOP) 12
  • 13.
    Operator Overloading Allows operators (e.g., +, - ) to be redefined for user-defined types. C++: operator + for Complex class to add two complex numbers. Types of Constructors Special methods for object initialization. Default Constructor: No arguments. Parameterized Constructor: Initializes with specific values. Copy Constructor: Copies state from another object. Polymorphism Types Compile-time: Achieved through overloading. Run-time: Achieved through overriding. Compile-time: Method overloading. Run-time: Method overriding. Four Pillars of Object-Oriented Programming (OOP) 13