简体   繁体   中英

How to impose contract to subclass from parent class implementation

If I have an interface and I want the functions in my interface to be implemented in my sub class but the parent class must implement the interface. And Sub class extends Parent Class.

Here is what I want.

interface MyInterface {
   public function find();
}


class B implements MyInterface {
}

class A extends B {
}

But an error is thrown saying find() function must be added in class B.

Anyone who could enlighten me on what am I doing wrong? thanks!

Interface is a structure, which contains a set of fields and methods that have to be implemented in every class that implements this interface. If your B class implements MyInterface it means it has to implement all of its methods/fields.

interface MyInterface
{
     public function find();
}

class B implements MyInterface {
    public function find()
    {
       echo "Hello world";
    }
}

Does A class have to implement this method too? I'll leave it as your homework.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM