OOP Cheat Sheet
OOP Cheat Sheet
Section titled “OOP Cheat Sheet”The Four Pillars
Section titled “The Four Pillars”| Pillar | Purpose | Implementation |
|---|---|---|
| Encapsulation | Data hiding + bundling | Private fields, public getters/setters |
| Abstraction | Hide complexity, show essentials | Abstract classes, interfaces |
| Inheritance | Code reuse via parent-child | extends keyword |
| Polymorphism | Many forms, same interface | Method overriding, interfaces |
Class & Object
Section titled “Class & Object”class ClassName { constructor(params) { /* initialize */ } method() { /* behavior */ } get property() { /* getter */ } set property(val) { /* setter */ } static staticMethod() { /* class-level */ }}const obj = new ClassName(args);Access Modifiers
Section titled “Access Modifiers”| Modifier | Same Class | Subclass | External |
|---|---|---|---|
public | ✅ | ✅ | ✅ |
protected | ✅ | ✅ | ❌ |
private | ✅ | ❌ | ❌ |
SOLID Principles
Section titled “SOLID Principles”| Letter | Name | One-Liner |
|---|---|---|
| S | Single Responsibility | One class = one job |
| O | Open/Closed | Extend without modifying |
| L | Liskov Substitution | Child must work as parent |
| I | Interface Segregation | Small, focused interfaces |
| D | Dependency Inversion | Depend on abstractions |
Relationships
Section titled “Relationships”| Type | UML | Lifetime | Example |
|---|---|---|---|
| Association | Simple arrow | Independent | Teacher → Student |
| Aggregation | Empty diamond ◊ | Independent | Department → Teachers |
| Composition | Filled diamond ◆ | Dependent | House → Rooms |
| Dependency | Dashed arrow | Temporary | OrderService → PaymentService |
Abstract Class vs Interface
Section titled “Abstract Class vs Interface”| Aspect | Abstract Class | Interface |
|---|---|---|
| Can have implementation? | Yes | No (traditionally) |
| Can have state? | Yes | No |
| Multiple inheritance? | No (extends one) | Yes (implements many) |
Design Principles
Section titled “Design Principles”| Principle | Meaning |
|---|---|
| DRY | Don’t Repeat Yourself |
| KISS | Keep It Simple, Stupid |
| YAGNI | You Ain’t Gonna Need It |
| Composition | Favor HAS-A over IS-A |
| Program to Interface | Depend on abstractions |
Key Code Snippets
Section titled “Key Code Snippets”Inheritance & super
Section titled “Inheritance & super”class Child extends Parent { constructor(params) { super(params); } method() { return super.method() + " extended"; }}Private Fields
Section titled “Private Fields”class BankAccount { #balance = 0; getBalance() { return this.#balance; } }Static Members
Section titled “Static Members”class MathUtils { static PI = 3.14159; static square(x) { return x * x; } }MathUtils.PI; // 3.14159Previous Topic: Interview Questions → Next Topic: Course Complete! 🎉