A Friendly Guide to Abstract Classes in TypeScript
- Nishadil
- September 20, 2026
- 0 Comments
- 3 minutes read
- 8 Views
- Save
- Follow Topic
What abstract classes are, why they matter, and how to use them with real‑world examples.
Learn the basics of abstract classes in TypeScript, see code samples, and discover best practices for clean, reusable OOP design.
Okay, let’s talk about abstract classes. If you’ve dabbled in object‑oriented programming you’ve probably heard the term, but in TypeScript they have a few quirks that make them especially handy.
First off, an abstract class is kind of like a blueprint. You can’t create an instance of it directly – think of it as a model you sketch before building the actual house. The class can hold concrete (real) methods, properties, even a constructor, but it can also declare abstract members that the subclasses must fill in.
Why bother? Imagine a bunch of related classes – say Dog, Cat, Bird – all of them can move() and eat(), but each makes a different sound. You could write move() once in a base class and force every animal to implement its own makeSound(). That’s the sweet spot of abstract classes: shared logic lives in one place, while the specifics stay in the child classes.
Here’s a tiny snippet to illustrate. The Animal class is abstract, it defines an abstract method makeSound() and a concrete method move():
abstract class Animal {
abstract makeSound(): void;
move(): void {
console.log('Moving...');
}
}
class Dog extends Animal {
makeSound(): void {
console.log('Bark');
}
}
const myDog = new Dog();
myDog.makeSound(); // Bark
myDog.move(); // Moving...
Notice we never do new Animal() – TypeScript will complain because the class is abstract. The Dog class inherits move() automatically and supplies its own makeSound() implementation.
Let’s break down the main features:
- Cannot be instantiated: trying to do
new Animal()throws a compile‑time error. - Abstract members: methods or properties marked
abstracthave no body; subclasses must provide one. - Concrete members: regular methods, properties, or constructors that already have an implementation.
- Polymorphism: a variable typed as the abstract base can hold any subclass instance, enabling flexible code.
Practical uses are everywhere. Want to enforce that every Shape knows how to calculate its area? Create an abstract Shape class with an abstract getArea() method, then let Circle, Rectangle, etc., implement it:
abstract class Shape {
abstract getArea(): number;
printArea(): void {
console.log(`The area is ${this.getArea()}.`);
}
}
class Circle extends Shape {
constructor(public radius: number) { super(); }
getArea(): number {
return Math.PI this.radius * 2;
}
}
const c = new Circle(5);
c.printArea(); // The area is 78.53981633974483
Notice how printArea() lives in the abstract class – every shape gets that for free, while the actual area calculation is left to the concrete subclass.
Another neat example is an abstract property. Suppose you have a Person class that knows it should have a name, but you don’t want to dictate how it’s stored. You can write:
abstract class Person {
abstract name: string;
display(): void { console.log(this.name); }
}
class Employee extends Person {
constructor(public name: string, public empCode: number) { super(); }
}
new Employee('James', 100).display(); // James
Here Employee satisfies the contract by providing a concrete name field.
Now, a few best‑practice nuggets:
- Use abstract classes when several classes share real, reusable logic.
- Declare members as abstract only when you truly need each subclass to supply its own version.
- Keep the abstract class focused on common behavior; avoid stuffing unrelated utilities inside it.
- Always instantiate the concrete subclass, never the abstract base.
All in all, abstract classes give you a tidy way to express “these things belong together, they share this, but each must do that.” They’re a cornerstone of clean, maintainable TypeScript code.
- India
- News
- Technology
- TechnologyNews
- Inheritance
- Polymorphism
- ObjectOrientedProgramming
- Typescript
- Encapsulation
- AbstractClass
- MandatoryImplementations
- AbstractMethods
- SharedFunctionality
- AbstractClassAndInterface
- AbstractProperty
- CommonInterface
- TypescriptQuestions
- ConcreteMethods
- CodeReuse
- SubclassImplementation
Editorial note: Nishadil may use AI assistance for news drafting and formatting. Readers can report issues from this page, and material corrections are reviewed under our editorial standards.