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

前端 未结 8 609
死守一世寂寞
死守一世寂寞 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 00:55

    No. I'm not sure why you'd quote the spec and then ask if there's any way to do the opposite of what the spec says...

    Perhaps if you explain why you want to do this, you could get some suggestions on how.

    0 讨论(0)
  • 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.
    }
    
    0 讨论(0)
提交回复
热议问题