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

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

    If you did this then DerivedOne would not be a Base, from the DerivedTwo's point of view. Instead what you want is a wrapper class

    //Uses myMethod but keeps it hidden
    public class HiddenBase {
        private final Base base = new Base();
        private void myMethod();
        public void otherMethod() {base.otherMethod();}
    }
    

    You can't access protected methods of the base though this way...

提交回复
热议问题