![]() |
Interface and Class in TypeScript are fundamental concepts for defining object structures and behavior, where interfaces provide a blueprint for properties and methods, and classes implement these interfaces to create objects with specific functionality and structure. These are the following methods: Interface Implemented by ClassIn this approach, an interface named Shape is defined with a method calFn() that calculates a shape’s area. The Rectangle class implements the Shape interface by providing its implementation of calFn(), the calculation of a rectangle’s area when an instance of Rectangle is created and calFn() is called on that instance. Syntax:class ClassName implements InterfaceName { Example: The below example demonstrates the Interface Implemented by the Class.
Output: Area of rectangle: 50 Multiple Interfaces Implemented by ClassIn this approach, a class Circle implements both the Shape and Color interfaces, allowing it to define methods for calculating area (calFn()) and specifying a color. When an instance of Circle is created, it can access methods from both interfaces, such as retrieving the color and calculating the area of the circle. Syntax:class ClassName implements Interface1, Interface2, ... { Example: The below example demonstrates the Multiple Interfaces Implemented by Class.
Output: Color of circle: red |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |