PHP|inheritance of all features and types in 2021 |
PHP|Inheritance All Features and Types
Inheritance: The process of getting the property of one class into another class is called Inheritance. In another word, we can say that the process of deriving a new class from an old class is called inheritance in which the new class is called derived or child or sub-class, and the old class is called Base or Parent or Super-class.
When a class inherits the property of a class it means it can access all the data member and member functions of that class except the private element. In this type of programming mainly two types of classes are used.
Parent/Super/Base class
Child/Sub/Derived class
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPyt6qtFTmS8cRqs3MdCCUeb0hbfi3lB4CHvzlrpYU6TPCPmOnjiJucYuHGlul1HleuyGqdX2V4fQ3dbrYxQCRgQ_CFrmIwmmhgmZ6Q9SE3qA167nDy_IRz-_sPLtF1DRFT3T3XIlTDOzy/w30-h14/newicon.gif)
Python Class Or Object
Parent/Super/Base class: The class which is inherited by another class is called Parent or Super or Base class.
Child/Sub/Derived class: The class which inherits the property of another class is called Child or Sub or Derived class.
Square extends Rectangle: Here Square is a Derived class and Rectangle is a Base class and extends is a keyword that is used to inherit one class into another.
How to inherit one class into another in PHP?
<?php class Rectangle { public function rec_area() { $h=20; $w=10; echo "Area of Rectangle:".($h*$w)."<br>"; } } class Square extends Rectangle { public function squ_area() { $side=20; echo "Area of Square:".($side*$side)."<br>"; } } $obj = new Square(); $obj->rec_area(); $obj->squ_area(); ?> *****OUTPUT***** Area of Rectangle:200 Area of Square:400 |
Here class Rectangle is a base class and Square is a derived class because Rectangle is inherited into Square therefore we can call all the functions using the object of Square.
Types of Inheritance in PHP: There are five types of inheritance in PHP.
1. single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
Single Inheritance: In this type of inheritance only two classes are used in which one is inherited by another.
How to use single inheritance in php?
<?php class Rectangle { public function rec_area() { $h=20; $w=10; echo "Area of Rectangle:".($h*$w)."<br>"; } } class Square extends Rectangle { public function squ_area() { $side=20; echo "Area of Square:".($side*$side)."<br>"; } } ?> $obj = new Square(); $obj->rec_area(); $obj->squ_area(); *****OUTPUT***** Area of Rectangle:200 Area of Square:400 |
In the above example, you can see that there are only two classes are used in which Rectangle is inherited by Square therefore using the object of Square we can call function rec_area() and squ_area().
Multiple Inheritance: When two or more than two classes are inherited by a single class simultaneously called multiple inheritances. In other words, we can say that in this type of inheritance Base class maybe two or more than two but the derived class should be one.
In this type of inheritance, at least three classes are compulsory. PHP does not support multiple inheritances therefore interface is used to implement multiple inheritances. the interface is declared with the interface keyword and it is implemented by a class while the class is extended by a class.
we can not define a function inside an interface, only can be declared. It is the responsibility of the derived class to implement/define the method of the interface.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPyt6qtFTmS8cRqs3MdCCUeb0hbfi3lB4CHvzlrpYU6TPCPmOnjiJucYuHGlul1HleuyGqdX2V4fQ3dbrYxQCRgQ_CFrmIwmmhgmZ6Q9SE3qA167nDy_IRz-_sPLtF1DRFT3T3XIlTDOzy/w30-h14/newicon.gif)
All Tkinter Functions
How to use multiple inheritances in php?
<?php interface Rectangle { public function rec_area(); } class Square { public function squ_area() { $side=20; echo "Area of Square:".($side*$side)."<br>"; } } class Triangle extends Square implements Rectangle { public function rec_area() { $h=20; $w=10; echo "Area of Rectangle:".($h*$w)."<br>"; } public function tri_area() { $b=25; $h=30; echo "Area of Triangle:".(0.5*$b*$h)."<br>"; } } $obj = new Triangle(); $obj->rec_area(); $obj->squ_area(); $obj->tri_area(); ?> *****OUTPUT***** Area of Rectangle:200 Area of Square:400 Area of Triangle:375 |
In the above example, you can see that there are two classes(Square and Triangle) and one interface (Rectangle) are used in which Square and Rectangle are inherited by Triangle, therefore, using object of Triangle we can call function rec_area(), squ_area() and tri_area().
Multilevel Inheritance: When the first class is inherited by the second class, the second class is inherited by the third class, and so on is called multilevel inheritance. In this type of inheritance, each derived class is the base class for the next class. In this type of inheritance, at least three classes are compulsory.
How to use multilevel inheritance in php?
<?php class Rectangle { public function rec_area() { $h=20; $w=10; echo "Area of Rectangle:".($h*$w)."<br>"; } } class Square extends Rectangle{ public function squ_area() { $side=20; echo "Area of Square:".($side*$side)."<br>"; } } class Triangle extends Square { public function tri_area() { $b=25; $h=30; echo "Area of Triangle:".(0.5*$b*$h)."<br>"; } } $obj = new Triangle(); $obj->rec_area(); $obj->squ_area(); $obj->tri_area(); ?> *****OUTPUT***** Area of Rectangle:200 Area of Square:400 Area of Triangle:375 |
Hierarchical Inheritance: When a single class is inherited by two or more than two classes simultaneously called hierarchical inheritance. In other words, we can say that in this type of inheritance derived class maybe two or more than two but the Base class should be one. In this type of inheritance, at least three classes are compulsory.
How to use hierarchical inheritance in php?
<?php class Rectangle { public function rec_area() { $h=20; $w=10; echo "Area of Rectangle:".($h*$w)."<br>"; } } class Square extends Rectangle{ public function squ_area() { $side=20; echo "Area of Square:".($side*$side)."<br>"; } } class Triangle extends Rectangle { public function tri_area() { $b=25; $h=28; echo "Area of Triangle:".(0.5*$b*$h)."<br>"; } } $obj = new Triangle(); $obj->rec_area(); $obj->tri_area(); ?> *****OUTPUT***** Area of Rectangle:200 Area of Triangle:375 |
In the above example, you can see that there are three classes(Rectangle, Square and Triangle) are used in which Rectangle is inherited by Square and Triangle, therefore, using object of Triangle we can call the function only rec_area() and tri_area() because there is no relation between Square and Triangle, therefore, function squ_area() can not be called by the object of Triangle. Similarly, by using the object of class Square we can call only function rec_area() and squ_area().
Hybrid Inheritance: The combination of
two or more than two inheritance is called Hybrid inheritance. It can be a combination of any two or
more than two inheritance(single, multiple, multilevel, hierarchical). In this type of inheritance, at least three classes are compulsory.
How to use hybrid inheritance in php?
<?php interface Rectangle { public function rec_area(); } class Square { public function squ_area() { $side=20; echo "Area of Square:".($side*$side)."<br>"; } } class Triangle extends Square implements Rectangle { public function rec_area() { $h=20; $w=10; echo "Area of Rectangle:".($h*$w)."<br>"; } public function tri_area() { $b=25; $h=30; echo "Area of Triangle:".(0.5*$b*$h)."<br>"; } } class Circle extends Triangle{ public function cir_area() { $r=2.5; echo "Area of Circle:".(3.14*$r*$r)."<br>"; } } $obj = new Circle(); $obj->rec_area(); $obj->squ_area(); $obj->tri_area(); $obj->cir_area(); ?> *****OUTPUT***** Area of Rectangle:200 Area of Square:400 Area of Triangle:375 Area of Circle:19.625 |
In the above example, you can see that there are four classes(Rectangle, Square, Triangle, and Circle) in which Rectangle and Square are inherited by Triangle class so in class Rectangle, Square and Triangle there is Multiple inheritances but class Triangle is inherited by Circle so in class Triangle and Circle there is Single inheritance. Therefore the above program is a combination of Multiple and Single inheritances so it is called Hybrid Inheritance.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPyt6qtFTmS8cRqs3MdCCUeb0hbfi3lB4CHvzlrpYU6TPCPmOnjiJucYuHGlul1HleuyGqdX2V4fQ3dbrYxQCRgQ_CFrmIwmmhgmZ6Q9SE3qA167nDy_IRz-_sPLtF1DRFT3T3XIlTDOzy/w30-h14/newicon.gif)
Java Abstraction|Inheritance
Advantage of Inheritance
1. Code Reusability: It means the function inside the base class is shared by all the derived classes.
2. Time-Saving: There is no need to define the existing property(same code) of a class in another class.
3. Less Cost: Existing code is reused, which leads to less development and maintenance costs.
4. It helps to reduce code redundancy.