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

前端 未结 6 2009
你的背包
你的背包 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:30

    1. Define a new Package
    2. Create a new interface (ie. HiddenOne) with scope "default" with a method "implementMe(HiddenOne)"
    3. Move Vehicle and Flier to the new Package.
    4. Inherit Vehicle and Flier from HiddenOne
    5. Implement the method implementMe in Vehicle.

    Now: Whenever you like to implement from "Flier" you must extends from Vehicle ! (because only Vehicle can implement implementMe).

    This is tricky but works great.

提交回复
热议问题