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

Abstract Classes की जानकारी

एक abstract class एक base class है जिसे directly instantiate नहीं किया जा सकता। यह shared behavior define कर सकती है और derived classes को abstract methods implement करने की ज़रूरत पड़ सकती है।
Syntax
typescript
abstract class ClassName {
  abstract method(): returnType;
}
class Concrete extends ClassName {
  method(): returnType {
    // implementation
  }
}

एक Abstract Class बनाना

एक abstract class abstract keyword से declare की जाती है और इसे extend करने के लिए design किया जाता है, directly instantiate करने के लिए नहीं — एक abstract class पर new करने की कोशिश एक compile-time error है।

उदाहरण: Creating an Abstract Class

typescript
abstract class Shape {
  abstract area(): number;
}
// new Shape(); // rejected: cannot instantiate an abstract class
class Circle extends Shape {
  constructor(public radius: number) { super(); }
  area() { return 3.14 * this.radius ** 2; }
}
console.log(new Circle(5).area());

Abstract Methods

एक abstract method की abstract base class में एक declaration होता है लेकिन कोई implementation नहीं; हर concrete subclass को अपनी implementation supply करनी ज़रूरी है, जिसे compiler enforce करता है।

उदाहरण: Abstract Methods

typescript
abstract class Animal {
  abstract makeSound(): string;
}
class Dog extends Animal {
  makeSound() { return "Woof"; }
}
console.log(new Dog().makeSound());

Abstract Classes में Concrete Methods

Abstract classes में abstract methods के साथ normal concrete methods भी हो सकते हैं, shared, already-working logic को base class में रहने देते हुए जबकि सिर्फ genuinely vary होने वाले parts subclasses के fill करने के लिए छोड़ दिए जाते हैं।

उदाहरण: Concrete Methods in Abstract Classes

typescript
abstract class Animal {
  abstract makeSound(): string;
  describe() {
    return `This animal says: ${this.makeSound()}`;
  }
}
class Cat extends Animal {
  makeSound() { return "Meow"; }
}
console.log(new Cat().describe());

Abstract Classes और Polymorphism

एक abstract class को अलग-अलग concrete subclasses के references के लिए एक type के रूप में उपयोग किया जा सकता है, इसलिए abstract type के against लिखा गया code uniformly काम करता है भले ही runtime पर कौन सी specific subclass pass की जाए।

उदाहरण: Abstract Classes and Polymorphism

typescript
abstract class Shape {
  abstract area(): number;
}
class Square extends Shape {
  constructor(public side: number) { super(); }
  area() { return this.side ** 2; }
}
class Circle extends Shape {
  constructor(public radius: number) { super(); }
  area() { return 3.14 * this.radius ** 2; }
}
const shapes: Shape[] = [new Square(4), new Circle(2)];
shapes.forEach((s) => console.log(s.area()));

Abstract Classes कब उपयोग करें

Abstract classes उपयोगी हैं जब related classes real implementation share करती हैं लेकिन उन्हें कम से कम एक method का अपना version भी देना होता है — अगर बिल्कुल कुछ भी shared नहीं है, तो आमतौर पर एक plain interface बेहतर fit है।

उदाहरण: When to Use Abstract Classes

typescript
abstract class Employee {
  constructor(public name: string) {}
  abstract calculateSalary(): number;
  printPaycheck() {
    console.log(`${this.name}: $${this.calculateSalary()}`);
  }
}
class Manager extends Employee {
  calculateSalary() { return 5000; }
}
new Manager("Alia").printPaycheck();
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. एक abstract class पर new Shape() try करना, जो एक error है क्योंकि abstract classes सिर्फ extend की जा सकती हैं।
  2. एक concrete subclass में एक abstract method implement करना भूल जाना, जो subclass को खुद compile fail करा देता है।
  3. एक abstract method के लिए एक body लिखना (abstract area() { }), जब abstract methods के पास सिर्फ एक signature होना चाहिए।

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.