5️⃣ 🧩 TypeScript — Interfaces & Classes (OOP)
Estimated reading: 4 minutes 28 views

🧱 TypeScript — Abstract Classes: Define Blueprint for Object-Oriented Design

🧲 Introduction – What Are Abstract Classes in TypeScript?

In TypeScript, an abstract class is a class that cannot be instantiated on its own and serves as a template or blueprint for other classes. It can contain both implemented methods and abstract methods (methods with no implementation). Abstract classes help you enforce structure and behavior in subclasses while enabling code reuse, polymorphism, and extensibility.

🎯 In this guide, you’ll learn:

  • What abstract classes are and how they work
  • Syntax for abstract properties and methods
  • How to extend and implement abstract classes
  • Differences between abstract classes and interfaces
  • Best practices and real-world examples

🧾 What Is an Abstract Class?

An abstract class is declared using the abstract keyword. It is designed to be inherited by other classes, not directly instantiated.

✅ Syntax:

abstract class Animal {
  abstract makeSound(): void;

  move(): void {
    console.log("Animal is moving...");
  }
}

Explanation:

  • makeSound() is an abstract method with no implementation.
  • move() is a regular method with shared logic.

🚫 Abstract Class Cannot Be Instantiated

const creature = new Animal(); // ❌ Error: Cannot create an instance of an abstract class

🔐 Abstract classes must be extended by a subclass that provides concrete implementations.


🧬 Implementing Abstract Methods in Subclasses

Subclasses must provide implementation for all abstract members from the parent class.

class Dog extends Animal {
  makeSound(): void {
    console.log("Woof!");
  }
}

const dog = new Dog();
dog.makeSound(); // Woof!
dog.move();      // Animal is moving...

Explanation:

  • Dog is a concrete class.
  • It provides a body for the makeSound() abstract method.

🧩 Abstract Properties

Just like methods, abstract classes can also declare abstract properties.

abstract class Shape {
  abstract name: string;

  describe(): void {
    console.log(`This is a ${this.name}`);
  }
}

class Circle extends Shape {
  name = "Circle";
}

const c = new Circle();
c.describe(); // This is a Circle

🧠 Abstract Class vs Interface

FeatureAbstract ClassInterface
Can contain method logic✅ Yes❌ No (only declarations)
Can have constructors✅ Yes❌ No
Can include access modifiers✅ Yes❌ No (only public structure)
Supports multiple inheritance❌ No (only one class)✅ Yes (multiple interfaces allowed)
Inheritance keywordextendsimplements

📌 Use abstract classes when you need shared implementation + structure.
📌 Use interfaces when only defining a structure with no logic.


📚 Real-World Use Cases

  • 📦 Shared service base classes (e.g., BaseHttpService)
  • 🧾 Document models (e.g., AbstractReport, AbstractInvoice)
  • 🚗 Component or UI base elements (BaseComponent, AbstractWidget)
  • 🎮 Game engine entities (Entity, Creature, EnemyBase)
  • 🛠️ Core engine logic with customizable hooks

⚠️ Common Mistakes & How to Avoid Them

❌ Mistake✅ Solution
Forgetting to implement abstract membersImplement all abstract methods and properties in subclasses
Trying to instantiate an abstract classCreate an instance of a subclass, not the abstract class
Using abstract where an interface is betterUse interfaces when only a structure is needed
Missing access modifiersUse public, protected, or private to control access

💡 Best Practices for Abstract Classes

  • ✅ Use abstract classes to define template behavior for subclasses
  • ✅ Use protected methods to expose base functionality to child classes
  • ✅ Combine with interfaces for hybrid contracts
  • ✅ Keep base classes focused—avoid excessive logic
  • ❌ Don’t overuse abstract classes where simpler patterns apply (e.g., function composition)

📌 Summary – Recap & Next Steps

Abstract classes in TypeScript offer a powerful way to design reusable, structured, and extensible class hierarchies. They let you define shared behavior while enforcing that certain methods or properties must be implemented by subclasses.

🔍 Key Takeaways:

  • Abstract classes define a contract and reusable logic
  • You cannot instantiate an abstract class directly
  • Subclasses must implement all abstract members
  • Use abstract for OOP-based design where inheritance and behavior sharing are needed

⚙️ Real-world relevance: Abstract classes are widely used in frameworks, design patterns, domain modeling, service layers, and object-based architecture.


❓ FAQs – Abstract Classes in TypeScript

❓ Can I instantiate an abstract class?
❌ No. Only concrete subclasses can be instantiated.

❓ Can an abstract class have constructors?
✅ Yes. Constructors are used to initialize shared state.

❓ Can a class extend an abstract class and implement interfaces?
✅ Yes. A class can extend one abstract class and implement multiple interfaces.

❓ Are abstract methods inherited?
📌 Yes, but they must be implemented in the subclass.

❓ What happens if a subclass does not implement all abstract members?
❌ TypeScript throws a compile-time error until all members are implemented.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

TypeScript — Abstract Classes

Or Copy Link

CONTENTS
Scroll to Top