Java: extending a class and implementing an interface that have the same method

前端 未结 4 1254
小蘑菇
小蘑菇 2021-02-05 04:39

Probably the following cannot be done (I am getting a compilation error: \"The inherited method A.doSomthing(int) cannot hide the public abstract method in B\"):



        
4条回答
  •  花落未央
    2021-02-05 05:11

    This has to do with visibility. You are using default (no modifier) visibility in C for myMethod but it needs to be public according to the interface B.

    Now you might think you used the default visibility for all of them, since in neither A, B, nor C did you explicitly select one of public, private, or protected. However, the interface uses public whether or not you explicitly indicate so.

提交回复
热议问题