Here, we will implement multi-level inheritance. In the multi-level inheritance, we will inherit the one base class into a derived class, and then the derived class will become the base class for another derived class.
The source code to demonstrate the multi-level inheritance is given below. The given program is compiled and executed successfully.
<?php
//PHP program to demonstrate the multi-level inheritance.
class Base
{
function BaseFun()
{
echo "BaseFun() called<br>";
}
}
class Derived1 extends Base
{
function Derived1Fun()
{
echo "Derived1Fun() called<br>";
}
}
class Derived2 extends Derived1
{
function Derived2Fun()
{
echo "Derived2Fun() called<br>";
}
}
$dObj = new Derived2();
$dObj->BaseFun();
$dObj->Derived1Fun();
$dObj->Derived2Fun();
?>
Output:
BaseFun() called
Derived1Fun() called
Derived2Fun() called
Explanation:
In the above program, we created three classes Base, Derived1, and Derived2. Here, we inherited the Base class into Derived1 class and then inherit the Derived1 class into Derived2 class using the extends keyword.
At last, we created the object $dObj of the Derived2 class. Then called the functions of all classes using the object of Derived2 class that will print appropriate messages on the webpage.
Program/Source Code:
The source code to demonstrate the multi-level inheritance is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we created three classes Base, Derived1, and Derived2. Here, we inherited the Base class into Derived1 class and then inherit the Derived1 class into Derived2 class using the extends keyword.
At last, we created the object $dObj of the Derived2 class. Then called the functions of all classes using the object of Derived2 class that will print appropriate messages on the webpage.
need an explanation for this answer? contact us directly to get an explanation for this answer