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

Here, we are going to learn how to inherit an abstract class into multiple non-abstract classes in PHP?
Submitted by Nidhi, on November 21, 2020

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.

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 Sample1, Sample2, 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.






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.