Is it possible to hide or lower access to Inherited Methods in Java?

前端 未结 8 613
死守一世寂寞
死守一世寂寞 2021-01-08 00:04

I have a class structure where I would like some methods in a base class to be accessible from classes derived directly from the base class, but not classes derived from der

8条回答
  •  臣服心动
    2021-01-08 01:02

    you have to make method final when override it

    public class Base {
    protected void myMethod() {}
    }
    
    // Uses myMethod and then hides it.
    public class DerivedOne extends Base {
    @Override
    final protected void myMethod(); //make the method final
    }
    
    
    public class DerivedTwo extends DerivedOne {
       // can't access myMethod here.
    }
    

提交回复
热议问题