Why can't implementing classes define an overriding method as static?

后端 未结 4 919
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-17 05:56

I\'m confused why the following is not allowed:

public interface MyInterface {
  MyInterface getInstance(String name);
}

public class MyImplementation imple         


        
4条回答
  •  醉酒成梦
    2020-12-17 06:25

    The answer comes down to what it means to implement an interface. When a class implements an interface, that is a promise that every instance of the class will respond to every method in the interface. When you implement the method as static, you make it possible to call the method without an instance of the class - but that doesn't fulfill the inheritance implementation's promise that the method will be callable on every instance of the class.

提交回复
热议问题