Q:

PHP program to inherit an abstract class into multiple non-abstract classes

belongs to collection: PHP Classes & Objects Programs

0

Here, we will create an abstract class that contains the abstract functions and then we will inherit the abstract class into multiple non-abstract classes and define the abstract function in all non-abstract 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 inherit an abstract class into multiple non-abstract classes is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

<?php
//PHP program to inherit an abstract class into 
//multiple non-abstract classes.
abstract class AbsClass
{
    public abstract function fun();
}

class Sample1 extends AbsClass
{
    public function fun()
    {
        printf("Sample1::fun() called<br>");
    }
}

class Sample2 extends AbsClass
{
    public function fun()
    {
        printf("Sample2::fun() called<br>");
    }
}

class Sample3 extends AbsClass
{
    public function fun()
    {
        printf("Sample3::fun() called<br>");
    }
}

$obj1 = new Sample1();
$obj2 = new Sample2();
$obj3 = new Sample3();

$obj1->fun();
$obj2->fun();
$obj3->fun();
?>

Output:

Sample1::fun() called
Sample2::fun() called
Sample3::fun() called

Explanation:

In the above program, we created an abstract class AbsClass that contains abstract functions fun().

After that, we created three non-abstract classes Sample1Sample2, and Sample3. Here, we used the extends keyword to inherit an abstract class in the non-abstract class.  Then we inherited the same abstract class into all non-abstract classes.

At last, we created three objects $obj1$obj2, and $obj3, 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 inherit an abstract class and an in... >>
<< PHP program to demonstrate the example of the simp...