PHP program to demonstrate the multi-level inheritance

Here, we are going to demonstrate the multi-level inheritance in PHP.
Submitted by Nidhi, on November 20, 2020

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.

Program/Source Code:

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.






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.