Here, we will implement single inheritance. In the single inheritance, we will inherit the one base class into a single derived class. We will use the extends keyword to implement inheritance.
The source code to demonstrate the single inheritance is given below. The given program is compiled and executed successfully.
<?php
//PHP program to demonstrate the single inheritance.
class Base
{
function BaseFun()
{
echo "BaseFun() called<br>";
}
}
class Derived extends Base
{
function DerivedFun()
{
echo "DerivedFun() called<br>";
}
}
$dObj = new Derived();
$dObj->BaseFun();
$dObj->DerivedFun();
?>
Output:
BaseFun() called
DerivedFun() called
Explanation:
In the above program, we created two classes Base and Derived. Here, we inherited the Base class into the derived class using the extends keyword.
The Base class contains a function BaseFun(), and Derived class also contains only one function DerivedFun().
At last, we created the object $dObj of the derived class. Then called BaseFun() and DerivedFun() using the object that will print the appropriate message on the webpage.
Program/Source Code:
The source code to demonstrate the single inheritance is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we created two classes Base and Derived. Here, we inherited the Base class into the derived class using the extends keyword.
The Base class contains a function BaseFun(), and Derived class also contains only one function DerivedFun().
At last, we created the object $dObj of the derived class. Then called BaseFun() and DerivedFun() using the object that will print the appropriate message on the webpage.
need an explanation for this answer? contact us directly to get an explanation for this answer