Inheritance का मतलब है कि एक class (child class) दूसरी class (parent class) की properties और methods को inherit (विरासत में प्राप्त) करती है।
दूसरे शब्दों में कहें तो, “Inheritance का मतलब होता है “विरासत” या “उत्तराधिकार”। यह एक ऐसा तरीका है जिसमें एक class दूसरी class की properties और methods को inherit (या reuse) कर सकती है।”
PHP में inheritance की मदद से एक class को दूसरे class से properties और methods मिलती हैं। इसका उपयोग code reusability को बढ़ाने के लिए किया जाता है, जिससे developers को code को बार-बार लिखने की जरूरत नहीं पड़ती।
PHP में Inheritance कैसे काम करता है?
Inheritance में दो classes होती हैं:-
- Parent Class (Base Class): जिस class को inherit किया जाता है, उसे Parent Class या Base Class कहते हैं।
- Child Class (Derived Class): जो class inherit करती है, उसे Child Class या Derived Class कहते हैं।
Inheritance का Syntax
PHP में inheritance के लिए extends keyword का उपयोग किया जाता है। जब एक class दूसरी class से inherit करती है, तो उसे parent class की सभी properties और methods अपने आप मिल जाती हैं।
class ParentClass {
// Properties and Methods
}
class ChildClass extends ParentClass {
// Additional Properties and Methods
}
इसका उदाहरण:–
<?php
// Parent Class
class Vehicle {
public $brand;
protected $color;
public function setBrand($brand) {
$this->brand = $brand;
}
protected function setColor($color) {
$this->color = $color;
}
public function getInfo() {
return "Brand: " . $this->brand . ", Color: " . $this->color;
}
}
// Child Class
class Car extends Vehicle {
private $model;
public function setModel($model) {
$this->model = $model;
}
public function setCarColor($color) {
$this->setColor($color); // Accessing protected method from Parent Class
}
public function getCarInfo() {
return $this->getInfo() . ", Model: " . $this->model;
}
}
// Creating object of Child Class
$car = new Car();
$car->setBrand("Toyota"); // Accessing public method from Parent Class
$car->setCarColor("Red"); // Accessing protected method through a public method in Child Class
$car->setModel("Corolla"); // Accessing Child Class method
echo $car->getCarInfo();
// Output: Brand: Toyota, Color: Red, Model: Corolla
?>
इस उदाहरण में,
- Vehicle class एक Parent Class है, जिसमें $brand और $color properties हैं, और setBrand(), setColor(), तथा getInfo() methods हैं।
- Car class एक Child Class है, जो Vehicle class को inherit करती है। इसमें एक अतिरिक्त property $model और method setModel() है।
- Child Class (Car) जो है वह Parent Class (Vehicle) के public और protected properties और methods को access कर सकती है।
- setCarColor() method के माध्यम से हम Parent Class के protected method setColor() को access करते हैं।
Inheritance के प्रकार
PHP में Inheritance निम्नलिखित प्रकार के होते हैं:-
1. Single Inheritance
Single inheritance तब होता है जब एक class दूसरी class से केवल अपनी properties और methods को inherit करती है। इसमें एक parent class और एक child class होती है। इसमें Child class, parent class की सभी properties और methods को अपने अंदर copy कर लेती है।
<?php
// Parent Class
class Vehicle {
public $brand;
public function setBrand($brand) {
$this->brand = $brand;
}
public function getBrand() {
return $this->brand;
}
}
// Child Class
class Car extends Vehicle {
public function drive() {
echo $this->brand . " car is driving.\n";
}
}
// Creating object of Child Class
$car = new Car();
$car->setBrand("Toyota");
echo $car->getBrand() . "\n"; // Output: Toyota
$car->drive(); // Output: Toyota car is driving.
?>
2. Multilevel Inheritance
Multilevel inheritance में एक class दूसरी class से inherit करती है, और फिर वह class तीसरी class को inherit करती है। यह एक chain (श्रृंखला) के रूप में काम करता है।
<?php
// Parent Class
class Animal {
public function eat() {
echo "Animal is eating.\n";
}
}
// Child Class 1
class Dog extends Animal {
public function bark() {
echo "Dog is barking.\n";
}
}
// Child Class 2
class Puppy extends Dog {
public function weep() {
echo "Puppy is weeping.\n";
}
}
// Creating object of Child Class 2
$puppy = new Puppy();
$puppy->eat(); // Output: Animal is eating.
$puppy->bark(); // Output: Dog is barking.
$puppy->weep(); // Output: Puppy is weeping.
?>
3. Hierarchical Inheritance
Hierarchical inheritance में एक parent class होती है, और एक से अधिक child classes होती हैं जो parent class से inherit करती हैं। इसमें एक ही parent class को बहुत सारीं child class इस्तेमाल करती हैं।
<?php
// Parent Class
class Shape {
public function draw() {
echo "Drawing a shape.\n";
}
}
// Child Class 1
class Circle extends Shape {
public function drawCircle() {
echo "Drawing a circle.\n";
}
}
// Child Class 2
class Square extends Shape {
public function drawSquare() {
echo "Drawing a square.\n";
}
}
// Creating objects of Child Classes
$circle = new Circle();
$square = new Square();
$circle->draw(); // Output: Drawing a shape.
$circle->drawCircle(); // Output: Drawing a circle.
$square->draw(); // Output: Drawing a shape.
$square->drawSquare(); // Output: Drawing a square.
?>
4. Hybrid Inheritance
जब दो या अधिक प्रकार के inheritance को मिलाकर इस्तेमाल किया जाता है, तो इसे Hybrid Inheritance कहा जाता है। यह विभिन्न प्रकार के inheritance का combination (संयोजन) होता है। जैसे कि– multilevel और multiple inheritance का combination (संयोजन)।
PHP में Hybrid Inheritance को achieve करने के लिए Traits का इस्तेमाल किया जाता है।
Example:
<?php
// Parent Class
class A {
public function methodA() {
echo "Method A from Class A.\n";
}
}
// Trait
trait B {
public function methodB() {
echo "Method B from Trait B.\n";
}
}
// Child Class
class C extends A {
use B; // Using Trait B
public function methodC() {
echo "Method C from Class C.\n";
}
}
// Creating object of Child Class
$obj = new C();
$obj->methodA(); // Output: Method A from Class A.
$obj->methodB(); // Output: Method B from Trait B.
$obj->methodC(); // Output: Method C from Class C.
?>
Inheritance के फायदे
इसके फायदे निम्नलिखित होते हैं:-
- Code Reusability: Inheritance का इस्तेमाल करके हम मौजूदा code को reuse कर सकते हैं, जिससे code duplication कम होती है।
- Code Organization: Inheritance से code को logical और hierarchical तरीके से organize किया जा सकता है।
- Extensibility: हम Parent Class के features को extend करके नई functionality (कार्यक्षमता) add कर सकते हैं।
इसे पढ़ें:-
निवेदन:- अगर आपके लिए यह पोस्ट उपयोगी रहा हो तो इसे अपने दोस्तों के साथ अवश्य share कीजिए, और अपने सवाल नीचे comment करके बताइए। धन्यवाद।