← Back to TypeScript Course | Chapter 6: Classes | Lesson 8 of 9

Classes में Interfaces Implement करना

एक class एक interface implement कर सकती है यह promise करने के लिए कि वह specific properties और methods provide करती है। यह class और इसे उपयोग करने वाले code के बीच एक clear contract बनाता है।
Syntax
typescript
class ClassName implements Interface1, Interface2 {
  // implement all members
}

Basic Interface Implementation

implements keyword एक class को एक interface से connect करता है, और compiler class को interface द्वारा declared हर property और method provide करने की ज़रूरत पड़ता है, या यह compile नहीं होगा।

उदाहरण: Basic Interface Implementation

typescript
interface Printable {
  print(): void;
}
class Report implements Printable {
  print() { console.log("Printing report"); }
}
new Report().print();

Interface Properties

एक interface उन properties को describe कर सकता है जो implementing classes को contain करनी चाहिए, TypeScript को class के against check करने के लिए एक structural contract देते हुए भले ही interfaces खुद कोई runtime code produce न करें।

उदाहरण: Interface Properties

typescript
interface Vehicle {
  wheels: number;
}
class Car implements Vehicle {
  wheels: number = 4;
}
console.log(new Car().wheels);

Interface Methods

Interfaces method signatures define कर सकते हैं जो implementing classes को provide करने ज़रूरी हैं, parameter और return types specify करते हुए बिना dictate किए कि method की body actually कैसे लिखी जाए।

उदाहरण: Interface Methods

typescript
interface Calculator {
  add(a: number, b: number): number;
}
class BasicCalculator implements Calculator {
  add(a: number, b: number): number {
    return a + b;
  }
}
console.log(new BasicCalculator().add(2, 3));

Multiple Interfaces Implement करना

एक class एक साथ एक से ज़्यादा interface implement कर सकती है, इसे कई independent contracts simultaneously satisfy करने देते हुए — उपयोगी जब एक class को, मान लीजिए, Comparable और Serializable दोनों के रूप में treat किया जाना ज़रूरी हो।

उदाहरण: Implementing Multiple Interfaces

typescript
interface Comparable {
  compareTo(other: number): number;
}
interface Serializable {
  serialize(): string;
}
class Score implements Comparable, Serializable {
  constructor(public value: number) {}
  compareTo(other: number) { return this.value - other; }
  serialize() { return String(this.value); }
}
const s = new Score(90);
console.log(s.compareTo(80), s.serialize());

Interfaces Implement क्यों करें

Interfaces class contracts को explicit बनाते हैं और अलग-अलग, unrelated classes को same structural shape follow करने देते हैं, जो generic code को उनमें से किसी के साथ interchangeably काम करने की permission देता है।

उदाहरण: Why Implement Interfaces

typescript
interface Shape {
  area(): number;
}
class Square implements Shape {
  constructor(public side: number) {}
  area() { return this.side ** 2; }
}
class Circle implements Shape {
  constructor(public radius: number) {}
  area() { return 3.14 * this.radius ** 2; }
}
function printArea(shape: Shape) {
  console.log(shape.area());
}
printArea(new Square(3));
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि implements code inherit करेगा, जब यह सिर्फ shape check करता है, इसलिए class को अब भी हर member खुद लिखना चाहिए।
  2. interface का एक member miss करना, जैसे print() छोड़ देना, जो class पर एक compile error देता है।
  3. यह मान लेना कि एक interface runtime पर दिखता है, इसलिए obj instanceof Printable fail होता है क्योंकि interfaces erased हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.