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

前端 未结 4 1209
小蘑菇
小蘑菇 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:02

    make the method public

    public class C extends A implements B {
    
        //trying to override doSomthing...
    
        public int myMethod(int x) {
            return doSomthingElse(x);
        }
    }
    

    interface methods are always public

    or just use composition instead of inheritance

提交回复
热议问题