Interface extends another interface but implements its methods

旧城冷巷雨未停 提交于 2019-12-29 10:36:08

问题


In java when an interface extends another interface:

  1. Why does it implement its methods?
  2. How can it implement its methods when an interface can't contain a method body
  3. How can it implement the methods when it extends the other interface and not implement it?
  4. What is the purpose of an interface implementing another interface?

This has major concepts in Java!

EDIT:

public interface FiresDragEvents {

  void addDragHandler(DragHandler handler);

  void removeDragHandler(DragHandler handler);
}


public interface DragController extends FiresDragEvents {

  void addDragHandler(DragHandler handler);

  void removeDragHandler(DragHandler handler);

  void dragEnd();

  void dragMove();
}

In eclipse there is the implement sign besides the implemented methods in DragController.

And when I mouse-hover it, it says that it implements the method!!!


回答1:


Why does it implement its methods? How can it implement its methods when an interface can't contain method body? How can it implement the methods when it extends the other interface and not implement it? What is the purpose of an interface implementing another interface?

Interface does not implement the methods of another interface but just extends them. One example where the interface extension is needed is: consider that you have a vehicle interface with two methods moveForward and moveBack but also you need to incorporate the Aircraft which is a vehicle but with some addition methods like moveUp, moveDown so in the end you have:

public interface IVehicle {
  bool moveForward(int x);
  bool moveBack(int x);
};

and airplane:

public interface IAirplane extends IVehicle {
  bool moveDown(int x);
  bool moveUp(int x);
};



回答2:


An interface defines behavior. For example, a Vehicle interface might define the move() method.

A Car is a Vehicle, but has additional behavior. For example, the Car interface might define the startEngine() method. Since a Car is also a Vehicle, the Car interface extends the Vehicle interface, and thus defines two methods: move() (inherited) and startEngine().

The Car interface doesn't have any method implementation. If you create a class (Volkswagen) that implements Car, it will have to provide implementations for all the methods of its interface: move() and startEngine().

An interface may not implement any other interface. It can only extend it.




回答3:


ad 1. It does not implement its methods.

ad 4. The purpose of one interface extending, not implementing another, is to build a more specific interface. For example, SortedMap is an interface that extends Map. A client not interested in the sorting aspect can code against Map and handle all the instances of for example TreeMap, which implements SortedMap. At the same time, another client interested in the sorted aspect can use those same instances through the SortedMap interface.

In your example you are repeating the methods from the superinterface. While legal, it's unnecessary and doesn't change anything in the end result. The compiled code will be exactly the same whether these methods are there or not. Whatever Eclipse's hover says is irrelevant to the basic truth that an interface does not implement anything.



来源:https://stackoverflow.com/questions/10227410/interface-extends-another-interface-but-implements-its-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!