Object Oriented Programming (OOP) Principles Including Encapsulation, Abstraction, Inheritance & Polymorphism Along with SOLID & Object Relationship Principles
-
Encapsulation
- Bundle data and methods; control access using
public
,private
, andprotected
. - Use getters/setters to validate or control access.
- Bundle data and methods; control access using
-
Abstraction
- Expose essential behavior via
interface
orabstract class
. - Hide implementation details so callers rely on stable contracts.
- Expose essential behavior via
-
Inheritance
- Reuse and extend behavior with
extends
andsuper
. - Model "is-a" relationships.
- Reuse and extend behavior with
-
Polymorphism
- Subclasses override methods; code works with base types or interfaces.
- Association: objects use each other.
- Aggregation: a has-a relationship; parts can exist independently.
- Composition: strong ownership; parts' lifecycle tied to the parent.
- SRP: One responsibility per class.
- OCP: Open for extension, closed for modification.
- LSP: Subtypes must be substitutable for their base types.
- ISP: Prefer many small, focused interfaces.
- DIP: Depend on abstractions, not concrete implementations.
// interface
interface Shape { area(): number; }
// abstract class
abstract class Animal {
protected name: string;
constructor(name: string) { this.name = name; }
abstract makeSound(): void;
}
// inheritance & polymorphism
class Dog extends Animal {
makeSound() { console.log('Woof'); }
}
// encapsulation
class BankAccount {
private balance = 0;
deposit(amount: number) { if (amount > 0) this.balance += amount; }
getBalance() { return this.balance; }
}