TL ; DR:
An abstract class is a class you can't instantiate directly — it's a half-finished template. It can declare methods with no body (abstract methods) that any concrete subclass must implement.
abstract class Animal {
abstract speak(): void;
}
class Dog extends Animal {
speak(): void { console.log("Woof"); }
}
Full lesson:
Sometimes you want to capture shared behavior across a family of classes without committing to one concrete implementation. For example, every Animal can speak(), but exactly how depends on whether it's a Dog or a Cat. That's what abstract classes are for.
Two superpowers
- You can't write
new Animal()directly — the compiler reports the errorCannot create an instance of an abstract class.You have to pick a concrete subclass. - Any subclass MUST implement every abstract method, or the compiler reports an error. The abstract class is a contract with teeth.
Mixing abstract and concrete
An abstract class can also have concrete methods (with bodies) and fields. Those are shared by all subclasses for free, just like normal inheritance. Only the methods marked abstract require an override.
Notes for TypeScript
- Put the keyword
abstracton the class and on each method that has no body:abstract speak(): void; - These checks happen at compile time. TypeScript reports the error, but the compiled JavaScript has no
abstract, so the check is not enforced at run time.
Your task
The abstract Animal class is already defined. Create a concrete Dog subclass and implement the speak method so it prints "Woof". The harness builds a Dog and calls speak().
Expected output:
WoofFree plan: 30 tutor messages. PRO is unlimited.
