TypeScript Abstraction : abstract Class In TypeScript In Hindi

📔 : TypeScript 🔗

TypeScript एक powerful superset है JavaScript का जो object-oriented programming (OOP) के important concepts जैसे encapsulation, inheritance, polymorphism, और abstraction को support करता है।

Abstraction एक ऐसा concept है जो OOP का foundation मन जाता है। इससे आप complex systems को unnecessary details को hide और essential functionality को expose करके simplify कर सकते हो।

इस topic में हम TypeScript Abstraction को समझेंगे, कैसे काम करता है, इसका syntax, use cases, और practical examples के साथ इसके implementation को देखेंगे।

What is Abstraction ?

Abstraction का basic मतलब है complexity को hide करना और सिर्फ relevant information को expose करना।

इसका purpose है कि आप सिर्फ वही चीजें show करते हैं जो necessary हैं, और unnecessary details को hide करते हैं। इससे code काफी clean और maintainable बन जाता है।

TypeScript में abstraction को abstract classes और interfaces के through implement किया जाता है। Abstract classes और interfaces developer को एक blueprint provide करते हैं, जिससे derived (child) classes implement करती हैं।

Key Points -
  • Abstraction का use कर के आप high-level functionality define करते हैं बिना implementation details के।

  • Abstract classes और interfaces को objects directly instantiate नहीं कर सकते, सिर्फ उन्हें extend या implement कर सकते हैं।

  • Derived classes abstract methods का concrete implementation provide करती हैं।

How does abstraction work in TypeScript ?

TypeScript में abstraction को implement करने के दो main तरीके होते हैं -

  • Abstract Classes : Abstract class एक incomplete class होती है जिसमे कुछ methods abstract होते हैं (मतलब उनका implementation नहीं होता), और कुछ methods implemented होते हैं।

  • Interfaces : Interface एक contract जैसा होता है जो classes को follow करना पड़ता है , Interfaces का implementation जो भी class करेगी, उससे interface के सारे methods को implement करना पड़ेगा।

TypeScript Abstract Class

TypeScript में abstract class एक base class होती है जिसमे abstract methods define किये जाते हैं।

Abstract methods सिर्फ signature provide करते हैं, और उनका implementation child classes करती हैं। आप abstract class का object directly नहीं बना सकते, लेकिन उससे extend करके derived classes बना सकते हैं।

Syntax of Abstract Class

abstract class ClassName { abstract methodName(): void; // Abstract method without implementation }
TypeScript Abstract Class Example

Let's suppose , आपको अलग-अलग devices का management करना है, जैसे TV, AC, या Refrigerator. हर device को turnOn() और turnOff() करना होता है, लेकिन specific behavior हर device का अलग होगा।

// Abstract class for Devices abstract class Device { // Abstract methods abstract turnOn(): void; abstract turnOff(): void; // Common method for all devices getStatus(): void { console.log("Getting device status..."); } } // TV Class inheriting Device class TV extends Device { turnOn(): void { console.log("Turning on the TV."); } turnOff(): void { console.log("Turning off the TV."); } } // Air Conditioner Class inheriting Device class AirConditioner extends Device { turnOn(): void { console.log("Turning on the Air Conditioner."); } turnOff(): void { console.log("Turning off the Air Conditioner."); } } // Using the classes const myTV = new TV(); myTV.turnOn(); // Output: Turning on the TV. myTV.getStatus(); // Output: Getting device status. myTV.turnOff(); // Output: Turning off the TV. const myAC = new AirConditioner(); myAC.turnOn(); // Output: Turning on the Air Conditioner. myAC.getStatus(); // Output: Getting device status. myAC.turnOff(); // Output: Turning off the Air Conditioner.
Explanation
  • Device एक abstract class है जिसमे turnOn() और turnOff() methods को abstract बनाया गया है, और getStatus() method को common behavior के लिए use किया गया है।

  • TV और AirConditioner classes ने turnOn() और turnOff() methods को अपने specific behavior के साथ implement किया।

यह abstraction का real-world example है जहाँ हम device management को abstract class के through control कर रहे हैं।

Abstraction with Interfaces In TypeScript

TypeScript में abstraction को implement करने का दूसरा तरीका है interfaces. Interface एक contract कि तरह काम करता है जो methods और properties को define करता है, लेकिन उनका implementation derived class के ऊपर होता है।

Interfaces inheritance के लिए एक flexible तरीका provide करते हैं, क्योंकि एक class multiple interfaces को implement कर सकती है।

TypeScript Abstract Class Example

Suppose , एक payment gateway system है, जहाँ अलग-अलग payment methods (like credit card, debit card, or UPI) का management होता है।

सभी payment gateways के pass processPayment() method होता है, लेकिन हर payment gateway का implementation different होता है।

// Interface for Payment Gateway interface PaymentGateway { processPayment(amount: number): void; } // Class for Credit Card Payment class CreditCardPayment implements PaymentGateway { processPayment(amount: number): void { console.log(`Processing credit card payment of Rs.${amount}`); } } // Class for UPI Payment class UpiPayment implements PaymentGateway { processPayment(amount: number): void { console.log(`Processing UPI payment of Rs.${amount}`); } } // Function to make payments using any payment gateway function makePayment(paymentGateway: PaymentGateway, amount: number) { paymentGateway.processPayment(amount); } // Using the classes const creditCardPayment = new CreditCardPayment(); makePayment(creditCardPayment, 5000); // Output: Processing credit card payment of Rs.5000 const upiPayment = new UpiPayment(); makePayment(upiPayment, 1500); // Output: Processing UPI payment of Rs.1500

Explanation

  • PaymentGateway एक interface है जिसमे processPayment() method define किया गया है।

  • CreditCardPayment और UpiPayment classes ने PaymentGateway को implement किया और अपना specific implementation provide किया।

  • makePayment() function generic है जो किसी भी payment gateway को handle कर सकता है, abstraction का use करके।

Abstract Classes vs Interfaces In TypeScript

Abstraction को TypeScript में implement करने के दो तरीके होते हैं — abstract classes और interfaces. दोनो का काम almost same होता है, लेकिन इनके बीच कुछ differences भी होते हैं।

FeatureAbstract ClassesInterfaces

Inheritance

एक class सिर्फ एक abstract class को extend कर सकती है।

एक class multiple interfaces को implement कर सकती है।

Implementation

Abstract class में methods के साथ implementation हो सकती है।

Interface में methods सिर्फ declare होते हैं, implementation नहीं।

Fields/Properties

Abstract classes में fields/properties हो सकती हैं।

Interfaces में properties declare कि जा सकती हैं, लेकिन कोई implementation नहीं होती।

When to Use

जब आपको re-usability और common functionality चाहिए हो।

जब आपको only method signatures और flexible implementation चाहिए हो।

Abstraction Real-World Example

एक और fresh example जो हम abstraction के लिए use कर सकते हैं, वो है smart home automation system.

इस system में अलग-अलग smart devices होते हैं, जैसे smart bulbs, smart speakers, या smart thermostats. हर device को control करने के लिए common functionalities होती हैं, लेकिन specific implementation हर device का अलग होता है।

// Abstract class for Smart Devices abstract class SmartDevice { abstract turnOn(): void; abstract turnOff(): void; // Common functionality for all smart devices connectToWiFi(): void { console.log("Connecting to Wi-Fi..."); } } // Smart Bulb Class inheriting SmartDevice class SmartBulb extends SmartDevice { turnOn(): void { console.log("Turning on the Smart Bulb."); } turnOff(): void { console.log("Turning off the Smart Bulb."); } } // Smart Speaker Class inheriting SmartDevice class SmartSpeaker extends SmartDevice { turnOn(): void { console.log("Turning on the Smart Speaker."); } turnOff(): void { console.log("Turning off the Smart Speaker."); } } // Using the classes const myBulb = new SmartBulb(); myBulb.connectToWiFi(); // Output: Connecting to Wi-Fi... myBulb.turnOn(); // Output: Turning on the Smart Bulb. myBulb.turnOff(); // Output: Turning off the Smart Bulb. const mySpeaker = new SmartSpeaker(); mySpeaker.connectToWiFi(); // Output: Connecting to Wi-Fi... mySpeaker.turnOn(); // Output: Turning on the Smart Speaker. mySpeaker.turnOff(); // Output: Turning off the Smart Speaker.

I Hope , अब आपको अच्छे से समझ आ गया होगा।

Happy coding 😍

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 4.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook