简体   繁体   中英

Extending an interface to another interface

Let's say I have 2 interfaces here, Moveable and Walkable (sorry for the bad example, but please if you have a better one kindly post it)

interface Runnable{
    void run();
}

interface Walkable extends Runnable {
    void walk();
}

public class Human implements Walkable {

}

And the interface Walkable is a subclass of Runnable , when the Human class implements the Walkable interface should the Human class provide implementations for void walk() from the interface Walkable and void run() from the interface Runnable ? does the interface Walkable inherit the abstract method run() from the interface Runnable ?

是的,您的Human类需要同时实现walk()和run()方法。

should the human class provide implementations for void walk from the interface walkable and void run() from the interface runnable?

Yes , when your Walkable interface extends Runnable it also inherits run method means now if Human class is implementing Walkable interface it has to implement both the methods otherwise it should be abstract .

Implementing an Interface is a contract where Implementing class has to implement all the methods declared in Interface.

does the interface Walkable inherit the abstract method run() from the interface Runnable?

Yes, It is the OOPS Inheritance concept.

when the Human class implements the Walkable interface should the human class provide implementations for void walk from the interface walkable and void run() from the interface runnable?

Yes. You could have discovered this very easily by trying to compile the code. The compiler would complain because there are no implementstions of the run and walk methods in class Human .

does the interface walkable inherits the abstract method run() from the interface runnable?

Yes.

Walkable是Runnable,因此必须定义run()函数。

Yes your human should walk AND run .

Which would mean to invert the thing no? If you run you can certainly walk.But someone injured could walk without running ...

yes, an interface can extend multiple interfaces and the implementing class should implement all the methods inherited by the implementing interface.

So yes to your question, you must implement both methods

Human class needs to implement both methods, otherwise - you must declare it abstract. And yes, Walkable interface inherits the method run() from Runnable interface.

You can learn more about interfaces and inheritance here .

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