What's the Difference Between 'extends' and 'implements' in TypeScript ?
Last Updated :
18 Jul, 2024
TypeScript offers powerful way for organizing code and managing relationships between different components through the use of extends and implements keywords. This article explores the distinctions and functionalities of these two keywords.
Extends
The extends keyword is used for two main purposes in TypeScript: inheriting from a class (class inheritance) and extending a type (type extension).
Class Inheritance
When using extends for class inheritance, a subclass is created that inherits properties and methods from another class.
Syntax (For class inheritance):
class DerivedClass extends BaseClass {
// Additional properties and methods in the derived class
}
Syntax (For type extension):
type ExtendedType = BaseType & {
// Additional properties and methods in the extended type
};
Example 1: In this example, we are using extends for class inheritance
JavaScript
class Animal {
makeSound(): void {
console.log('Dog sound');
}
}
// Derived class representing a
// specific type of animal: Dog
class Dog extends Animal {
bark(): void {
console.log('Dog is barking!');
}
}
const myDog = new Dog();
myDog.makeSound();
myDog.bark();
Output:
Dog sound
Dog is barking!
Example 2: In this example, we are using extends for type extension.
JavaScript
type Shape = {
color: string;
};
type Square = Shape & {
sideLength: number;
};
const mySquare: Square = {
color: 'red',
sideLength: 5,
};
console.log(mySquare);
Output:
{ color: 'red', sideLength: 5 }
Implements
The implements keyword is used to ensure that a class conforms to a particular interface. This enforces that the class implements all the properties and methods defined by the interface.
Syntax:
class ClassName implements InterfaceName {
// Class properties and methods
}
Example: In this example, the GFG
class implements the GeeksForGeeks
interface, and it is required to provide an implementation for the print
method specified by the interface.
JavaScript
interface GeeksforGeeks {
print(): void;
}
class GFG implements GeeksforGeeks {
print() {
console.log('GeeksForGeeks');
}
}
const output = new GFG();
output.print();
Output:
GeeksForGeeks
Difference between extends and implements
Features | extends | Implements |
---|
Inheritance | Extends is used for class inheritance. It allows a class to inherit properties and methods from another class. | Implements is used for interface implementation. It enables a class to provide specific implementations for the methods defined in an interface. |
---|
Multiple Inheritance | A class can extend only one class | A class can implement multiple interfaces |
---|
Implementation of Methods | No direct implementation of methods. | Requires the class to provide concrete implementations for all methods declared in the interface. |
---|
Code Reusability | Promotes code reusability | Promotes code reusability and abstraction through interfaces. |
---|
Abstract Classes | Can extend abstract classes | Cannot extend abstract classes but can implement abstract methods. |
---|
Similar Reads
What is the difference between 'String' and 'string' in TypeScript ? Unlike JavaScript, TypeScript uses static typing, i.e. it specifies what kind of data the variable will be able to hold. Since TypeScript is a superscript of JavaScript, it also holds a distinction between a string and String. The usage of a string object in JS (or TS for that matter) is very minima
4 min read
What is the difference between interface and type in TypeScript ? In TypeScript, both interface and type are used to define the structure of objects, but they differ in flexibility and usage. While interface is extendable and primarily for object shapes, type is more versatile, allowing unions, intersections, and more complex type definitions.Type in TypeScriptThe
3 min read
Difference Between Internal & External Modules in TypeScript In TypeScript, modules are a way to organize code and encapsulate functionality. There are two types of modules: internal modules (also known as namespaces) and external modules (also known as modules). Internal Modules (Namespaces):Internal modules, known as namespaces, provide a way to organize co
2 min read
Difference between TypeScript and JavaScript Ever wondered about the difference between JavaScript and TypeScript? If you're into web development, knowing these two languages is super important. They might seem alike, but they're actually pretty different and can affect how you code and build stuff online.In this article, we'll break down the
4 min read
Difference between interfaces and classes in TypeScript In this article, we will see what is the Difference between "interface" and "Classes" in TypeScript. Interface: Interface is the virtual structure that is used for type-checking. In TypeScript we use interface keyword to create the new interface with identity. It create the structure for the same da
3 min read