Is there a way to guarantee an interface extends a class in Java?

前端 未结 6 1961
你的背包
你的背包 2020-12-28 13:38

Suppose I have the following situation:

public abstract class Vehicle {
  public void turnOn() { ... }
}

public interface Flier {
  public void fly();
}
         


        
6条回答
  •  有刺的猬
    2020-12-28 14:23

    This question shows that you haven't grasped the essence of interface and class. Forgetting the concrete Java syntax right now, all you need to understand first is that: interface is a set of protocol, which should be implementation-agnostic. It makes no sense to let an interface extend a class(which is implementation-oriented).

    Back to your concrete question, if you want to guarantee that a Flier is always a kind of Vehicle, just change the latter to an interface and let former extends it(It does make sense to extend one protocol from the other protocol). After that, you may create any class(abstract or concrete) that implements Vehicle or Flier.

提交回复
热议问题