Falak
TypeScript

Abstract Class in Typescript

1 min read

#Inside:

  • Abstract class vs Concrete class.

  • What are abstract methods ?

  • Overriding concrete methods of Abstract class.


#Abstract class vs Concrete (non-abstract) class:

A Abstract class cannot be instantiated directly using the new keyword. Instead it is a contract for other sub-classes to extend.

When a subclass extends an abstract class, it is forced to adhere to that blueprint by implementing all the abstract methods, while safely inheriting all the pre-written concrete code.

Take the below example:

JS
// Define the blueprint
abstract class Employee {
  // 1. Properties
  constructor(public name: string, protected id: number) {}

  // 2. Concrete method (has a real implementation)
  getDetails(): string {
    return `Employee: ${this.name} (ID: ${this.id})`;
  }

  // 3. Abstract method (no body, must be implemented by subclasses)
  abstract calculateSalary(): number;
}

// Inherit from the abstract class
class FullTimeEmployee extends Employee {
  constructor(name: string, id: number, private monthlySalary: number) {
    super(name, id); // Must call the parent constructor
  }

  // Implement the required abstract method
  calculateSalary(): number {
    return this.monthlySalary;
  }
}

// Usage
// const emp = new Employee("Alice", 101); // ❌ Error: Cannot create an instance of an abstract class.

const john = new FullTimeEmployee("John Doe", 402, 5000);

console.log(john.getDetails());       // Output: Employee: John Doe (ID: 402)
console.log(john.calculateSalary());   // Output: 5000

FullTimeEmployee is extending the abstract class Employee.See, getDetails is concrete method any kind of employee but calculateSalary which can be implemented based on the type of employee (FullTimeEmployee or PartTimeEmployee).

Lets see the definition of Abstract methods.


#Abstract methods

  • Abstract methods can only be defined inside an abstract class.

  • When a subclass extends an abstract class, it is forced to adhere to that blueprint by implementing all the abstract methods, while safely inheriting all the pre-written concrete code.


#Overriding concrete methods of Abstract class.

In this scenario, we have a base PaymentProcessor abstract class. Most payment types follow a standard process, but a specific type (like Crypto) requires extra steps (like blockchain network fee calculations), meaning it needs to override the standard concrete behavior.

JS
abstract class PaymentProcessor {
  constructor(protected amount: number) {}
JS
// 1. Abstract method: Every subclass MUST implement this uniquely
  abstract processPayment(): void;
JS
// 2. Concrete method: Default behavior shared by most subclasses
  calculateTotal(): number {
    const processingFee = 2.50; // Standard flat fee
    return this.amount + processingFee;
  }
}
JS
// ==========================================
// Subclass 1: Uses the default concrete method
// ==========================================
class CreditCardProcessor extends PaymentProcessor {
  processPayment(): void {
    console.log(`Charging $${this.calculateTotal()} to Credit Card.`);
  }
}
JS
// ==========================================
// Subclass 2: Overrides the concrete method
// ==========================================
class CryptoProcessor extends PaymentProcessor {
  // OVERRIDE: Crypto has different fee rules
  calculateTotal(): number {
    const networkGasFee = this.amount * 0.01; // 1% dynamic gas fee
    return this.amount + networkGasFee;
  }
JS
processPayment(): void {
    console.log(`Requesting $${this.calculateTotal()} from Crypto Wallet.`);
  }
}
JS
// ==========================================
// Execution
// ==========================================
const order1 = new CreditCardProcessor(100);
order1.processPayment();
// Output: Charging $102.5 to Credit Card. (Uses base standard fee of 2.50)
JS
const order2 = new CryptoProcessor(100);
order2.processPayment();
// Output: Requesting $101 from Crypto Wallet. (Uses overridden 1% fee rule)
Share this post