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

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

    You could rearrange your classes and interfaces like this:

    public interface IVehicle {
      public void turnOn();
    }
    
    public abstract class Vehicle implements IVehicle {
      public void turnOn() { ... }
    }
    
    public interface Flier extends IVehicle {
      public void fly();
    }
    

    This way all implementations of Flier are guaranteed to implement the protocol of a vehicle, namely IVehicle.

提交回复
热议问题