Q:

PHP program to demonstrate the inheritance of interfaces

belongs to collection: PHP Classes & Objects Programs

0

Here, we will inherit an interface into another interface using the extends keyword and then implement the derived interface into the 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 interfaces is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

<?php
//PHP program to demonstrate the inheritance of interfaces.
interface Inf1
{
    public function Fun1();
}
interface Inf2 extends Inf1
{
    public function Fun2();
}

class Sample implements Inf2
{
    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 interfaces Inf1Inf2, and a class Sample. The Inf1 interface contains the declaration of Fun1(), and the Inf2 interface contains the declaration of Fun2().

Here, we inherited the Inf1 interface into Inf2 and then implemented the Inf2 interface into 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 example of the simp... >>
<< PHP program to implement multiple-inheritance usin...