Classes and Interfaces in TypeScript: The Difference
Definition and Purpose
-
Interfaces:
- Interfaces are used in TypeScript to define the structure and type of an object.
- They solely define the object's type without providing implementation.
- It serves as a strict structural contract for defining property and method signatures, but does not include method implementations.
- Interfaces are commonly used to enforce that classes or objects adhere to a specific structure.
-
Classes:
- Classes are blueprints for objects, defining the data and methods that can operate on that data.
- In TypeScript, classes not only define properties and methods but also include the concrete implementation of these methods.
- Classes can be instantiated to create concrete objects.
Implementation Details
-
Interfaces:
- Interfaces are abstract and cannot contain any concrete implementation code.
- Interfaces allow defining which methods and properties a class must implement, without concern for the specific implementation details.
-
Classes:
- Classes contain actual implementation code that specifically implements the methods declared in the interface.
- Classes can be instantiated to create concrete objects that can have state and behavior.
Inheritance and Polymorphism
-
Interfaces:
- Support multiple interface inheritance, allowing a class to implement multiple interfaces to combine properties and methods from multiple interfaces.
- Interfaces can inherit from each other, enabling reuse of properties and methods from other interfaces.
-
Classes:
- Classes support single inheritance, meaning a class can inherit from only one parent class but can implement multiple interfaces.
- Classes can extend functionality by inheriting from another class, enabling code reuse.
Examples
-
Interface Example:
typescriptinterface IAnimal { name: string; makeSound(): void; } class Dog implements IAnimal { name: string; constructor(name: string) { this.name = name; } makeSound() { console.log("Woof! Woof!"); } }In this example, the
IAnimalinterface defines the properties and methods that all animals should have, while theDogclass implements this interface by providing concrete method implementations. -
Class Example:
typescriptclass Animal { name: string; constructor(name: string) { this.name = name; } makeSound() { console.log("Some sound!"); } } class Cat extends Animal { constructor(name: string) { super(name); } makeSound() { console.log("Meow! Meow!"); } }In this example,
Animalis a base class providing basic constructors and methods. TheCatclass inherits fromAnimaland overrides themakeSoundmethod to provide a cat-specific implementation.
Summary
Interfaces provide a mechanism to ensure that certain classes have specific methods without concerning themselves with implementation details. Classes are the structures that concretely implement these methods. Using interfaces helps define clear contracts between different classes, while classes are used to implement these functionalities and behaviors.