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

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.

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 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();




Fun1() called
Fun2() called


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.

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

© some rights reserved.