Change the access modifier of an overridden method in Java?

后端 未结 5 1660
-上瘾入骨i
-上瘾入骨i 2020-12-28 15:32

Is there a reason one can change the access modifier of an overridden method? For instance,

abstract class Foo{
    void start(){...}
}

And

5条回答
  •  粉色の甜心
    2020-12-28 16:16

    Edit: OK, I changed my answer to fix the problem.

    If that couldn't be done, then there would be some cases where a class wouldn't be able to implement an iterface and extend a class because they have the same method with different access modifiers.

    public Interface A {
      public void method();
    }
    
    public abstract classs B {
      protected void method();
    }
    
    public class AB extends B implements A {
      /*
       * This would't be possible if the access modifier coulnd't be changed
       * to less restrictive
      */
      public void method();
    }
    

提交回复
热议问题