Q:

PHP program to demonstrate the inheritance of abstract classes

belongs to collection: PHP Classes & Objects Programs

0

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.

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 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 Abs1Abs2, 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.

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 method overriding... >>
<< PHP program to inherit an abstract class and an in...