PHP में Abstract Class एक प्रकार की class होती है, जिसे हम सीधे instantiate नहीं कर सकते। अर्थात् इसका इस्तेमाल objects को create करने के लिए नहीं किया जा सकता।
Abstract Class को केवल एक blueprint की तरह use किया जाता है, जिसे हम दूसरे class में extend करते हैं। इसका उपयोग तब किया जाता है जब हम एक base class बनाना चाहते हैं, जिसमें कुछ common functionality होती है, लेकिन उस class के सीधे object (ऑब्जेक्ट) नहीं बनाना चाहते हैं।
Abstract class में कुछ methods को बिना implement किए define किया जाता है। इन methods को abstract methods कहते हैं। Abstract methods वे methods होते हैं जिनका केवल declaration होता है, implementation नहीं।
Abstract Class की Syntax
PHP में abstract class को डिफाइन करने के लिए abstract keyword का प्रयोग किया जाता है। अगर किसी class में एक या एक से अधिक abstract methods होते हैं, तो उस class को भी abstract declare करना होता है।
abstract class ClassName {
// Regular method
public function regularMethod() {
// Method implementation
}
// Abstract method
abstract public function abstractMethod();
}
- abstract class: यह कीवर्ड abstract class को define करता है।
- abstract method: यह method केवल declare किया जाता है, implement नहीं। इसे derived class में implement करना जरूरी होता है।
Abstract Class का उदाहरण
abstract class Animal {
// Regular method
public function sleep() {
echo "Animal is sleeping.\n";
}
// Abstract method
abstract public function makeSound();
}
class Dog extends Animal {
// Implementing the abstract method
public function makeSound() {
echo "Dog is barking.\n";
}
}
class Cat extends Animal {
// Implementing the abstract method
public function makeSound() {
echo "Cat is meowing.\n";
}
}
// Creating objects
$dog = new Dog();
$dog->makeSound(); // Output: Dog is barking.
$dog->sleep(); // Output: Animal is sleeping.
$cat = new Cat();
$cat->makeSound(); // Output: Cat is meowing.
$cat->sleep(); // Output: Animal is sleeping.
इस उदाहरण में:
- Animal एक abstract class है जिसमें एक सामान्य method sleep() और एक abstract method makeSound() है।
- Dog और Cat class Animal class को extend करती हैं और makeSound() method को implement करते हैं। हम Animal class का object नहीं बना सकते, लेकिन Dog और Cat के objects बना सकते हैं।
Abstract Method क्या है?
Abstract methods वो methods होती हैं जिनकी कोई body नहीं होती। इन methods को child class में implement किया जाता है।
Abstract methods का उद्देश्य होता है कि child class को एक विशेष method signature दिया जाए जिसे वो अपनी जरूरत के हिसाब से implement कर सके।
abstract class Animal {
// Abstract method without body
abstract protected function makeSound();
}
class Dog extends Animal {
// Implementing the abstract method
public function makeSound() {
echo "Bark!";
}
}
class Cat extends Animal {
// Implementing the abstract method
public function makeSound() {
echo "Meow!";
}
}
यहां makeSound() एक abstract method है, जिसे Dog और Cat class में implement किया गया है।
Abstract Class के कुछ महत्वपूर्ण Points
- Abstract class को सीधे instantiate नहीं किया जा सकता। यानि कि इनका इस्तेमाल objects बनाने के लिए नहीं किया जा सकता।
- अगर एक class में कम से कम एक abstract method है, तो उस class को abstract class ही declare करना होगा।
- Derived class को सभी abstract methods को implement करना जरूरी होता है।
- Abstract class में abstract methods के अलावा सामान्य methods और properties भी हो सकते हैं।
निवेदन:– अगर आपके लिए यह आर्टिकल useful रहा हो तो इसे अपने friends के साथ share कीजिए। और आपके किसी भी subjects से संबंधित कोई भी सवाल हो तो उसे नीचे comment करके बताइए। मैं उन सवालों को आसान भाषा में post बनाकर इस वेबसाइट में publish कर दूंगा। धन्यवाद।