What does the 'implements' keyword do, and how does it differ from 'extends'?
Short answer
implements requires a class to satisfy an interface's shape — providing all its declared members — without inheriting any implementation or behavior.
Advertisement
AModel answer
implements requires a class to satisfy an interface's shape — providing all its declared members — without inheriting any implementation or behavior. extends inherits actual behavior and state from a parent class (or interface, for interface-to-interface extension), and a class can only extend one class but implement multiple interfaces.
interface Flyable { fly(): void; }
interface Swimmable { swim(): void; }
class Duck implements Flyable, Swimmable {
fly() { console.log('flying'); }
swim() { console.log('swimming'); }
}Real-world example
Requiring a class to conform to a Serializable or Comparable-style contract without forcing a specific inheritance hierarchy.
Likely follow-ups
- Does 'implements' add any runtime type checking, or is it purely a compile-time contract?
Related topics