Interface Inheritance and Extension: extends implements

In TypeScript, interfaces are a core tool for defining object structures. The ability to inherit and extend interfaces using the extends and implements keywords is a key technology for building complex class hierarchies. This article delves into the implementation principles and practical application scenarios of these two mechanisms.

1. Inheritance between Classes with extends

The extends keyword is used to implement inheritance relationships between classes, allowing subclasses to inherit attributes and methods from parent classes.

  • Syntax:
    class Child extends Parent {
      // Implementation in the subclass
    }
    
  • Characteristics:
    • Subclasses automatically inherit all non-private (public) attributes and methods of the parent class.
    • The constructor of the parent class can be invoked using super.
  • Practice:
    Used to build hierarchical class structures, such as a User class inheriting from a BaseUser class in an e-commerce system.

2. Association between Classes and Interfaces with implements

The implements keyword is used to implement interfaces (interface), requiring the class to provide all methods defined within that interface.

  • Syntax:
    class MyClass implements MyInterface {
      // Implementation of interface methods
    }
    
  • Characteristics:
    • The class must fully implement all methods in the interface; no method can be omitted.
    • Abstract methods can be defined with an abstract marker (requiring implementation by subclasses).
  • Practice:
    Used to define standardized data structures, such as a logistics system's Order class implementing the Orderable interface.

3. Comparison of Inheritance and Implementation

Feature extends implements
Relationship Class with Class Class with Interface
Methods Automatically inherited Must be implemented
Limitation Can override methods Cannot override already defined methods
Example User inherits from BaseUser Order implements Orderable

4. Practical Case: Building an Extensible System

Scenario: Developing an e-commerce system that supports various payment methods.

  • Steps:
    1. Define the Payment interface:
      interface Payment {
        pay(amount: number): void;
      }
      
    2. Implement specific payment methods:
      class CreditCard implements Payment {
        pay(amount: number) {
          console.log(`Paid ${amount} via Credit Card`);
        }
      }
      
    3. Extend payment methods using inheritance and implementation:
      class PayPal implements Payment {
        pay(amount: number) {
          console.log(`Paid ${amount} via PayPal`);
        }
      }
      
  • Advantages:
    • New payment methods can be extended without modifying existing code.
    • The implements keyword ensures that the payment logic adheres to specifications.

5. Summary

TypeScript's interface extension mechanism (extends and implements) provides flexibility and maintainability for class structures. By designing inheritance relationships and implementing interfaces reasonably, developers can build modular and extensible systems. In actual development, it is crucial to pay attention to the boundaries between inheritance and implementation to avoid errors caused by missing methods or overriding logic.