Why is it not allowed to narrow down scope of a method while overriding

后端 未结 4 1846
北海茫月
北海茫月 2021-01-17 09:27

In Java, when I override a method the compiler flags off any attempt to narrow down the visibility as an error. For ex: I can\'t override a public method as protected, while

4条回答
  •  北海茫月
    2021-01-17 10:03

    While over ridding or implementing the access level, we should go for same access level or wider to that access level.

    private < (default) < protected < public
    

    Public is wider most level.

    In Interface all members are default public. So, While implementing or over ridding we have to go for only public.

提交回复
热议问题