Abstraction โ Define contracts, hide complexity
Callers talk to a small surface; complexity lives behind it. ยท Interfaces / abstract classes document the contract.
Watch
Watch, then scroll down for code and practice.
In code
interface Shape {
area(): number;
}
class Circle implements Shape {
constructor(private r: number) {}
area() {
return Math.PI * this.r * this.r;
}
}
function totalArea(shapes: Shape[]) {
return shapes.reduce((s, x) => s + x.area(), 0);
}๐ Key ideas
What it is
Hiding implementation complexity behind a simple interface. Callers know WHAT an object does, not HOW it does it.
Abstract class vs Interface
Use an abstract class when subclasses share code. Use an interface when you only need a contract (no shared state or logic).
Example
A Shape interface declares area() and perimeter(). Circle, Square, and Triangle implement it differently โ the caller doesn't care.
Benefit
You can swap implementations (e.g. MySQL โ Postgres) without touching callers โ they only depend on the abstraction.
๐ง Practice โ Apply What You Learned
๐ Now apply what you learned
Pick a problem above, write your solution, and get AI feedback on your design.
Start Practice โ