# Abstract Classes & Methods

> Create strict blueprints for subclasses.

Subject: TypeScript | Lesson 44 of 54 | 4 min read
Interactive lesson: https://codewithanimation.in/typescript/abstract-classes-methods
Previous: [Parameter Properties](https://codewithanimation.in/typescript/parameter-properties.md) | Next: [Modern Decorators](https://codewithanimation.in/typescript/decorators.md)

## The Blueprint

An `abstract` class is a base class that cannot be instantiated directly. It exists solely to be extended by other classes.

Inside an abstract class, you can define `abstract` methods. These are method signatures without an implementation. Any subclass *must* provide the actual implementation for these methods.

## Forced Contracts

If you are building a game with different types of Enemies, they might all share some common logic (like `health` and a `takeDamage` method).

But how they attack should be unique to each enemy. By making the base `Enemy` class abstract, and giving it an `abstract attack()` method, you force every specific enemy subclass to define its own attack behavior, while safely sharing the health logic.

## Syntax

```typescript
abstract class Animal {
  // Shared implementation
  move() { console.log("Moving"); }
  
  // Required blueprint
  abstract makeSound(): void;
}

// Error! Cannot create an instance of an abstract class.
// const a = new Animal(); 

class Dog extends Animal {
  // Must implement this!
  makeSound() { console.log("Woof!"); }
}
```

## Common mistake: Interfaces vs Abstract Classes

Why not just use an `interface`? Interfaces are purely structural contracts. They contain *zero* implementation.

Abstract classes are used when you want a mix of both: you want to provide *some* default behavior (like `move()`), but enforce a contract for other behaviors (like `makeSound()`).

## Tip: Abstract Properties

You can also make properties abstract! `abstract readonly type: string;` will force subclasses to declare that property.

## Try it: Try it

Watch the compiler enforce the rules. Try instantiating the abstract base class directly, and see what happens when the subclass forgets to implement the required method.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/abstract-classes-methods

## Check yourself

### Question 1

What happens if a subclass extends an abstract class but fails to implement one of its abstract methods?

- A. The method defaults to a no-op (doing nothing).
- B. TypeScript throws a compile-time error.
- C. It throws a runtime error when the method is called.

Correct answer: B

TypeScript enforces the contract strictly at compile time. If you promise to extend an abstract class, you must implement all of its abstract members, or the subclass itself must also be declared abstract.

- Why not C: It fails long before runtime.

## Remember this

- Abstract classes cannot be instantiated with `new`.
- Abstract methods have no body and MUST be implemented by subclasses.
- They are perfect for sharing common logic while forcing specific custom behavior in subclasses.
- Use interfaces when there is no shared logic at all.
