PHP program to demonstrate the hierarchical or tree inheritance

Here, we are going to demonstrate the hierarchical or tree inheritance in PHP.
Submitted by Nidhi, on November 20, 2020

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

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 Base, Derived1, and Derived2. Here, we inherited the Base class into both Derived1, Derived2 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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.