What is Inheritance
Zaryab Khan
Co-Founder @ Soft Consultants | Blockchain | web3 | App Development | Web Development | SEO | AI development | Education Consultancy | Sale Generation | Business Development | IT Sales
OOP (object-oriented programing) in PHP
Object-oriented programming is one of the most important programming paradigms. OOP is dependent upon the concept of Objects and Classes. This concept provides a reusability feature to the programmer so there is no need to write code again and again.
There are a lot of important concepts of the OOP but one of the most important topics is inheritance.
?Inheritance
??It is the way of creating a new class from the existing class in such a way that the new class can access all the properties and features of an existing class. New Classes have all the properties of the existing class so it is important to create an object of a new class. Inheritance provides the feature of reusability of code to the programmer. The most important feature of inheritance is that overloading is only possible through inheritance.??
There are the following two types of inheritance.
Single-level inheritance
Multi-level inheritance
?
Single level inheritance?
In single-level inheritance, there is only one base class and one drive class. So it is not possible to inherit more than one drive class from the base class in the single level inheritance. And in single-level inheritance drive class can access all the properties of the base class.
Example program of Single level inheritance?
??<?php?
class X {?
public function getItem($string) {
?echo "Hi.." . $string;?
}
?public function getPHP() { echo "how are you" . PHP_EOL;
?}
??}?
class Y extends X
{
public function getItem($string) { echo "hello.." . $string . PHP_EOL; }
?public function getPHP() {
?echo "How are you";?
}?
????}?
$a = new A();?
$b = new B();?
$a->getItem("JOHN");?
$a->getPHP();
?$b->getItem("ZACK");
?$b->getPHP();?
?>
OUTPUT
Hi.. JOHN
how are you
领英推荐
hello… ZACK
How are you
?
Multi-level inheritance???????
In?Multi-level inheritance, a class is inherited from the drive class creating a driving class a base class for a new class. In simple words, the drive class creates another drive class. Multi-level inheritance is an oblique method of achieving multiple inheritances.
Example program of Multi-level inheritance?
<?php?
class X {?
public function getMyage()
?{?
return "I am 21";
???????}
???}
?class Y extends X {
?public function getMysonage()?
{ return "John is 22";
?}
?}?
class Z extends Y
?{?
public function getMygrandsonage() {?
return "ZACK is 23";?
}?
public function getmyHistory() {?
echo "Class X " .$this->getMyage();
?echo "Class Y ".$this-> getMysonage();
?echo "Class Z " . $this->getMygrandsonage();?
}
?}
?$obj = new Z();
?$obj->getmyHistory();?
?>
OUTPUT
I am 21
John is 22
ZACK is 23
?
Conclusion
Hopefully that this article helps you to learn the concept of OOP. And what is inheritance how to do inheritance between the classes and what are the uses of inheritances and you will about what are the types of inheritance like Single level inheritance and Multi-level inheritance? And learn How to use these inheritance within the code and what the limitation of inheritances are.