Interface extends another interface but implements its methods

后端 未结 3 2039
既然无缘
既然无缘 2020-12-23 11:14

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 cont
3条回答
  •  执笔经年
    2020-12-23 12:15

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

提交回复
热议问题