← Writing

Difference Between Interface and Abstract Class - OOPS

Basic Difference

Interface Abstract Class
An Interface defines a contract that a class must follow.
1. It contains only method signatures and property types.
2. No Implementation.
3. Used for defining shape of objects.
An abstract class is a class that:
1. Cannot be instantiated directly.
2. Can have both abstract methods and concrete methods.
3. Can contain state (properties with values) and access modifiers.

Examples

  1. Interface:
    interface NotificationProvider {
      send(to: string, message: string): Promise<void>;
    }
    
    class EmailProvider implements NotificationProvider {}
    class SMSProvider implements NotificationProvider {}
    
  2. Abstract Class:
    abstract class PaymentService {
      protected apiKey: string;
    
      constructor(apiKey: string) {
        this.apiKey = apiKey;
      }
    
      abstract processPayment(amount: number): Promise<boolean>;
    
      logTransaction(amount: number) {
        console.log(`Transaction logged: ${amount}`);
      }
    }
    

Use Cases

Interface Abstract Class
1. Define a contract for multiple implementations.
2. Create pluggable architectures.
1. When basic structure + default behaviour is needed.

Details

Feature Interface Abstract Class
Implementation allowed? ❌ ✅
Can have constructor? ❌ ✅
Can have access modifiers? ❌ ✅
Multiple inheritance? ✅ ❌
Used for Structure Base behavior + structure
Runtime existence? ❌ Removed after compile ✅ Exists in JS

Using both together.

interface Payment {
  process(): void;
}

abstract class BasePayment implements Payment {
  abstract process(): void;

  log() {
    console.log("Logging...");
  }
}