PHP Tutorial

PHP Examples

PHP Practice

PHP program to demonstrate the inheritance of abstract classes

Here, we are going to demonstrate the inheritance of abstract classes in PHP.
Submitted by Nidhi, on November 21, 2020 [Last updated : March 13, 2023]

Inheritance of Abstract Classes

Here, we will inherit an abstract class into another abstract class using the extends keyword and then inherit the second abstract class into a non-abstract class.

PHP code to implement inheritance of abstract classes

The source code to demonstrate the inheritance of abstract classes is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

<?php
//PHP program to demonstrate the inheritance 
//of abstract classes.
abstract class Abs1
{
    public abstract function Fun1();
}
abstract class Abs2 extends Abs1
{
    public abstract function Fun2();
}

class Sample extends Abs2
{
    function Fun1()
    {
        printf("Fun1() called<br>");
    }

    function Fun2()
    {
        printf("Fun2() called<br>");
    }
}

$obj = new Sample();

$obj->Fun1();
$obj->Fun2();

?>

Output

Fun1() called
Fun2() called

Explanation

In the above program, we created two abstract classes Abs1, Abs2, and a non-abstract class Sample. The Abs1 abstract class contains an abstract function Fun1(), and the Abs2 abstract class contains an abstract class Fun2().

Here, we inherited the Abs1 abstract class into Abs2 and then inherited the Abs2 abstract class into the Sample class.

At last, we created an object $obj of Sample class and called functions that will print the appropriate message on the webpage.

PHP Class & Object Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.