PHP OOPs In Hindi – Inheritance In PHP In Hindi

PHP

PHP OOPs In HindiInheritance In PHP In Hindi – PHP Inheritance Allows Karta Hai Hame Ki Class Define Karo Uske Bad Ham Inherits Kar Sakte Hai All Methods And Properties Yani Dusri Class Ke |

For Example:- Derived Class Kar Sakta Hai Parent Class Ke Properties Ko |

Ek Bar Ek Parent Class Ko Create Karne Ke Bad Ham Parent Class (Base Class ) Ko Ham Derived Class (Sub Class ) Me Istemal kar Sakte Hain And Parent Class Or Derived Class Ke Features To Alag-Alag Honge Hi. Lekin Ham Derived Class Se Parent Class Ke Features Ka Istemal Kar Sakte Hain |

Read Also:- PHP Access Modifiers

Inheritance In PHP In Hindi

Inheritance OOP Ka Ek Part Hota Hain And Inheritance Mai Ek Se Jyada Classes Hote Hain |

Inheritance In PHP in Hindi Ko Understand Karne Ke Liye Upper Diye Image Ka Example Dekhte Hain

Jaise Aap Dekh Sakte Hai Hamne Ek Parent Class (Vehicle) Create Ki Hain Or Vehicle Mai Koi Type Ke Vahical Hote Unke Liye Hamne Derived Class (Car, Bus, Bike ) Alag- Alag Create Ki Or Ab Inhe Derived Class Se Parent Class Ke Features Ka Istemal Kar Sakte Hain.

Read Also :- PHP Constructor And Destructor

Inheritance Ko 2 Class Dena Jaruri Hota Hain |

  • Base Class(Parent): Base class Ko parent class Bhi Kaha Jata Hain |
  • Derived Class(Child): Derived class Ko sub-Ya child class Bhi Kaha Jata Hain| Derived class Ye Base class Ki properties ya attributes Ko inherit Karta Hai |

PHP Me Class Ko Inherit Karne Ke Liye extends Keyword Ka Istemal Kiya Jata Hai |

Syntax:

class Base{
	Base_Class_Body
  }
class Derived extends Base{
	Derived_Class_Body
  }

Example 1:

<?php
class Car {
  public $name;
  public $color;
  
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  public function intro() {
    echo "The car is {$this->name} and the color is {$this->color}.";
  }
}

// Details is inherited from Car
class Details extends Car {
  public function message() {
    echo "Its Car ? ";
  }
}
$exp = new Details("Audi", "red");
$exp->message();
$exp->intro();
?>

Output:

Its Car ? The car is Audi and the color is red.

Example 2:

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  protected function intro() {
    echo "The fruit is {$this->name} and the color is {$this->color}.";
  }
}

class Strawberry extends Fruit {
  public function message() {
    echo "Am I a fruit or a berry? ";
    // Call protected method from within derived class - OK
    $this -> intro();
  }
}

$strawberry = new Strawberry("Strawberry", "red"); // OK. __construct() is public
$strawberry->message(); // OK. message() is public and it calls intro() (which is protected) from within the derived class
?>

Output:

Am I a fruit or a berry? The fruit is Strawberry and the color is red.

Also Read:

Friends Mujhe Umeed Hai Ki Aapko PHP Inheritance In Hindi Ke Bare Mai 100% Jankari Ho Gayi Hogi | Agar Aapko Learn Karne Main Dikkat Aa Rahi Hai To Aap Mere Se Contact Kar Sakte Hai |

Leave a Reply

Your email address will not be published.