Q:

PHP program to demonstrate the hierarchical or tree inheritance

belongs to collection: PHP Classes & Objects Programs

0

Here, we will implement hierarchical or tree inheritance. In the hierarchical inheritance, we will inherit the one base class into multiple derived classes.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to demonstrate the hierarchical inheritance is given below. The given program is compiled and executed successfully.

<?php
//PHP program to demonstrate the hierarchical inheritance.
class Base
{
    function BaseFun()
    {
        echo "BaseFun() called<br>";
    }
}

class Derived1 extends Base
{
    function Derived1Fun()
    {
        echo "Derived1Fun() called<br>";
    }
}

class Derived2 extends Base
{
    function Derived2Fun()
    {
        echo "Derived2Fun() called<br>";
    }
}

$Obj1 = new Derived1();
$Obj2 = new Derived2();

$Obj1->BaseFun();
$Obj1->Derived1Fun();

echo "<br>";

$Obj2->BaseFun();
$Obj2->Derived2Fun();

?>

Output:

BaseFun() called
Derived1Fun() called

BaseFun() called
Derived2Fun() called

Explanation:

In the above program, we created three classes BaseDerived1, and Derived2. Here, we inherited the Base class into both Derived1Derived2 classes.

At last, we created the objects of Derived1 and Derived2 class and called the methods that will print the appropriate messages on the webpage.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Classes & Objects Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
PHP program to demonstrate the example of a simple... >>
<< PHP program to demonstrate the multi-level inherit...