Skip to content

OOP Cheat Sheet

PillarPurposeImplementation
EncapsulationData hiding + bundlingPrivate fields, public getters/setters
AbstractionHide complexity, show essentialsAbstract classes, interfaces
InheritanceCode reuse via parent-childextends keyword
PolymorphismMany forms, same interfaceMethod overriding, interfaces

class ClassName {
constructor(params) { /* initialize */ }
method() { /* behavior */ }
get property() { /* getter */ }
set property(val) { /* setter */ }
static staticMethod() { /* class-level */ }
}
const obj = new ClassName(args);

ModifierSame ClassSubclassExternal
public✅✅✅
protected✅✅❌
private✅❌❌

LetterNameOne-Liner
SSingle ResponsibilityOne class = one job
OOpen/ClosedExtend without modifying
LLiskov SubstitutionChild must work as parent
IInterface SegregationSmall, focused interfaces
DDependency InversionDepend on abstractions

TypeUMLLifetimeExample
AssociationSimple arrowIndependentTeacher → Student
AggregationEmpty diamond ◊IndependentDepartment → Teachers
CompositionFilled diamond ◆DependentHouse → Rooms
DependencyDashed arrowTemporaryOrderService → PaymentService

AspectAbstract ClassInterface
Can have implementation?YesNo (traditionally)
Can have state?YesNo
Multiple inheritance?No (extends one)Yes (implements many)

PrincipleMeaning
DRYDon’t Repeat Yourself
KISSKeep It Simple, Stupid
YAGNIYou Ain’t Gonna Need It
CompositionFavor HAS-A over IS-A
Program to InterfaceDepend on abstractions

class Child extends Parent {
constructor(params) { super(params); }
method() { return super.method() + " extended"; }
}
class BankAccount { #balance = 0; getBalance() { return this.#balance; } }
class MathUtils { static PI = 3.14159; static square(x) { return x * x; } }
MathUtils.PI; // 3.14159

Previous Topic: Interview Questions → Next Topic: Course Complete! 🎉