What object-oriented programming means: classes as blueprints, objects as instances, inheritance and overriding, and interfaces as promises, in TypeScript.
OOP explained by cats. Kitty makes a blueprint. It's called a kitty class. The class says every kitty has a name, every kitty has a color, every kitty can meow. From this class, we make kitties. Fluffy, whiskers, shadow. They all follow the same rules, but each is unique.
We call them instances of a class. Kitty makes a tiger class. Tiger inherits from kitty. Tigers still have names. Tiger look different. Tiger big. Meow scary, still kitty. Finally, interfaces. All kitties agree. If you're a kitty, you must meow.
Some meow loud, some meow soft. But every kitty must meow. That's object -oriented programming. Classes are the blueprints. Inheritance is passing traits from one kitty class to a new kitty class. Interfaces are promises all cats must keep.
Object-oriented programming (OOP) organises a program around objects: bundles of data and the actions that go with that data. Three ideas do most of the work, and they are the ones the video covers: classes, inheritance and interfaces.
Objects bundle data with behaviour
Without OOP, a program about cats might keep names in one list, colours in another, and have a loose meow function that takes a name. Nothing ties those pieces together, so it's easy to pass the wrong name or update one list and forget the other.
An object keeps the related pieces in one place. A cat object holds its own data (its state, such as a name and a colour) and the things it can do (its behaviour, such as meowing). The data are usually called properties or fields, and the functions attached to an object are called methods.
Classes are blueprints
A class describes what every object of one kind has and can do. It isn't a cat itself; it's the plan for making cats. Each object built from it is an instance of the class.
Here is a Kitty class in TypeScript:
class Kitty { name: string; colour: string; constructor(name
OOP explained by cats. Kitty makes a blueprint. It's called a kitty class. The class says every kitty has a name, every kitty has a color, every kitty can meow. From this class, we make kitties. Fluffy, whiskers, shadow. They all follow the same rules, but each is unique.
We call them instances of a class. Kitty makes a tiger class. Tiger inherits from kitty. Tigers still have names. Tiger look different. Tiger big. Meow scary, still kitty. Finally, interfaces. All kitties agree. If you're a kitty, you must meow.
Some meow loud, some meow soft. But every kitty must meow. That's object -oriented programming. Classes are the blueprints. Inheritance is passing traits from one kitty class to a new kitty class. Interfaces are promises all cats must keep.
The constructor runs once, when new Kitty(...) creates an instance. Its job is to set the starting state.
this means 'the object this method was called on'. Inside fluffy.meow(), this.name is "Fluffy"; inside shadow.meow(), it's "Shadow".
Both instances share one definition of meow, but each keeps its own name and colour. Changing fluffy.colour leaves shadow alone.
That is what a class is for: write the rules once, then make as many objects as you need, each following those rules with its own data.
Inheritance builds new classes from old ones
Inheritance lets one class start from another and change or add only what's different. The new class is the subclass (or child), and the one it builds on is the superclass (or parent).
Tiger didn't declare name, colour or a constructor, yet a tiger has all three: it inherited them from Kitty. It did declare its own meow, which overrides the parent's version. When you call stripes.meow(), the language looks for meow on Tiger first and finds the roar. Only if Tiger had no meow would it fall back to the one on Kitty.
A subclass can also call the parent's version with super, which helps when you want to extend behaviour rather than replace it:
class Lion extends Kitty { meow(): string { return super.meow() + ", then roars"; }}
The 'is-a' test
Inheritance models an is-a relationship: a tiger is a kitty, so anywhere the program expects a Kitty, a Tiger should work. If you can't say 'X is a Y' and mean it, inheritance is probably the wrong tool. A CatBasket might hold a kitty, but it isn't one, so it shouldn't extend Kitty just to borrow its name property.
Interfaces are promises
An interface lists what a type must be able to do, without saying how. It's a contract: any class that implements the interface promises to provide those methods, and the compiler holds it to that promise.
Kitten and Robocat share no code and no parent class. What they share is the promise. Leave meow out of Robocat and TypeScript refuses to compile it.
The pay-off comes when code depends on the interface instead of a particular class:
function wakeUp(cats: Meower[]) { for (const cat of cats) { console.log(cat.meow()); }}wakeUp([ new Kitten(), new Robocat(), new Tiger("Raja", "orange"),]);// mew// BEEP MEOW// Raja says ROAR
wakeUp doesn't know or care what kind of cat it has. Each one meows in its own way, loud or soft, and the function still works. This is polymorphism: one call, cat.meow(), with different behaviour depending on the object behind it.
Tiger fits too, even though it never wrote implements Meower. TypeScript checks interfaces by shape, so anything with a matching meow method counts. Java and C# are stricter: a class must name the interface to count as one.
Interfaces are also how dependency injection usually works: a class asks for 'something that can send email' rather than one specific mailer, so a test can hand it a fake.
Interface or parent class?
Both let different classes be used the same way, but they solve different problems:
A parent class shares code. Subclasses get working methods and data for free, and they're tied to that parent for good.
An interface shares only a shape. It carries no code, so unrelated classes can each implement it their own way. In Java, C# and TypeScript, a class can extend only one parent but can implement many interfaces.
Many languages also have abstract classes, which sit in between: a class you can't create instances of directly, with some methods written and others left for subclasses to fill in.
Two more terms you'll meet
Classes, inheritance and interfaces are the core of OOP. Two more words come up alongside them all the time:
Encapsulation: hiding an object's internal state and letting other code change it only through its methods. In TypeScript, marking a property private restricts direct access during type checking, so outside code cannot normally assign a kitty's hunger directly. This is not runtime protection; JavaScript's # private fields provide that.
Abstraction: showing what an object does and hiding how. wakeUp only needs to know that a cat can meow, not how each one does it.
With inheritance and polymorphism, these make up what are often called the four pillars of OOP.
When OOP fits, and when it doesn't
OOP works well when a program is about things whose state changes over time: users and their accounts, shapes on a canvas, enemies in a game. Grouping each thing's data with the code allowed to change it makes a large codebase easier to find your way around. Java and C# are built around it, and it's common in Python, TypeScript, Kotlin and C++.
It fits less well when the work is mostly turning data from one form into another, such as a script that reads rows, filters them and adds them up. There, plain functions over plain data are often shorter and easier to test, and wrapping each step in a class adds ceremony for no gain. Most modern languages let you mix the two styles, and most real projects do.
Common mistakes
Deep inheritance chains:Animal, then Mammal, Cat, HouseCat and Kitten looks tidy, but a change near the top ripples through everything below it, and finding where a method is really defined means climbing the tree. One or two levels is usually plenty.
Inheriting just to reuse code: If the is-a test fails, use composition instead. Give the class a property that holds the other object, and call that. 'Prefer composition over inheritance' is common advice for this reason, and the decorator pattern is one well-known way to apply it.
Mixing up the class and an instance:Kitty is the blueprint; fluffy is a cat. Changing an instance's own name or colour leaves the other instances alone. Objects referenced by several instances can still share mutable state.
Forgetting super() in a subclass constructor: A subclass that writes its own constructor must call super(...) before it uses this. TypeScript won't compile it otherwise, and plain JavaScript throws an error.
Overriding in a way that breaks the promise: If every kitty's meow returns a string, a subclass whose meow throws an error instead will break code that trusted any kitty to meow.
Key takeaways
A class is a blueprint. Each object made from it is an instance with its own data and the class's shared methods.
Inheritance builds a subclass from a parent, keeping what it has and overriding what should differ. Use it only where the is-a test holds.
An interface is a promise of methods with no code behind it. Any class that keeps it can be used wherever the interface is expected.
Code written against an interface works with every object that keeps the promise: that's polymorphism.
Keep hierarchies shallow and reach for composition when the is-a test fails.