Interface extends another interface but implements its methods

后端 未结 3 2049
既然无缘
既然无缘 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 11:57

    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.

提交回复
热议问题