← Back to TypeScript Course | Chapter 4: Interfaces | Lesson 5 of 7

Interfaces को Implement करना

एक class एक interface implement कर सकता है यह guarantee देने के लिए कि वह required properties और methods provide करता है। यह TypeScript को एक clear contract देता है जिसे एक class follow करे।
Syntax
typescript
class ClassName implements InterfaceName {
  property: type = value;
}

Basic Implementation

एक class declaration में implements InterfaceName जोड़ना require करता है कि वह class interface defines करने वाली हर property और method को satisfy करे।

अगर class में interface को चाहिए कुछ missing है, या इसके लिए गलत type है, TypeScript एक compile error report करता है।

उदाहरण: Basic Implementation

typescript
interface Greetable {
  name: string;
}
class Person implements Greetable {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
}
console.log(new Person("Ana").name);

Implementing Methods

एक interface implement करने वाले class को हर required method के लिए एक compatible implementation provide करना चाहिए, interface के declared parameter और return types को match करते हुए।

Class की अपनी implementation ज़्यादा specific हो सकती है, पर कभी interface के promise से कम capable नहीं।

उदाहरण: Implementing Methods

typescript
interface Speaker {
  speak(): string;
}
class Robot implements Speaker {
  speak(): string {
    return "Beep boop";
  }
}
console.log(new Robot().speak());

Implementing Multiple Interfaces

एक single class एक साथ कई interfaces implement कर सकता है, simply उन्हें implements के बाद comma-separated list करके। यह एक class को एक साथ कई independent contracts satisfy करने देता है, जैसे एक साथ Comparable और Serializable होना।

उदाहरण: Implementing Multiple Interfaces

typescript
interface Flyable {
  fly(): string;
}
interface Swimmable {
  swim(): string;
}
class Duck implements Flyable, Swimmable {
  fly() { return "Flying"; }
  swim() { return "Swimming"; }
}
const duck = new Duck();
console.log(duck.fly(), duck.swim());

Using Implemented Classes

एक given interface implement करने वाले किसी भी class का instance उस interface type की उम्मीद वाली किसी भी जगह उपयोग किया जा सकता है, class के अपने concrete name की परवाह किए बिना।

यही है जो interfaces को code लिखने के लिए उपयोगी बनाता है जो एक specific implementation के बजाय एक shape पर depend करता है।

उदाहरण: Using Implemented Classes

typescript
interface Speaker {
  speak(): string;
}
class Robot implements Speaker {
  speak() { return "Beep"; }
}
function announce(speaker: Speaker) {
  console.log(speaker.speak());
}
announce(new Robot());

Implementing Extended Interfaces

जब एक class ऐसा interface implement करता है जो खुद दूसरे interfaces को extend करता है, class को उस interface द्वारा directly जोड़े गए requirements और इसके parents से inherited everything, दोनों को satisfy करना चाहिए।

TypeScript पूरे combined contract को check करता है, सिर्फ interface के अपने declared members को नहीं।

उदाहरण: Implementing Extended Interfaces

typescript
interface Named {
  name: string;
}
interface Greetable extends Named {
  greet(): string;
}
class Person implements Greetable {
  constructor(public name: string) {}
  greet() { return `Hi, I'm ${this.name}`; }
}
console.log(new Person("Zoe").greet());
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. एक class पर implements Speaker लिखना पर speak() method न जोड़ना, जो एक compile error है।
  2. implements को extends के साथ confuse करना और class को interface से code inherit करने की उम्मीद करना।
  3. एक method को एक अलग signature के साथ implement करना, जैसे number return करना जब interface कहता है string।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

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.