Abstract Classes की जानकारी
In this page:
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
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
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
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
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
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();
- एक abstract class पर
new Shape()try करना, जो एक error है क्योंकि abstract classes सिर्फ extend की जा सकती हैं। - एक concrete subclass में एक abstract method implement करना भूल जाना, जो subclass को खुद compile fail करा देता है।
- एक abstract method के लिए एक body लिखना (
abstract area() { }), जब abstract methods के पास सिर्फ एक signature होना चाहिए।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: