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

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

    Inheritance works because everywhere you can use the base class, you can also use one of it's subclasses. The behavior may be different, but the API is not. The concept is known as the Liskov substitution principle.

    If you were able to restrict access to methods, the resulting class would not have the same API and you would not be able to use substitute an instance of the base class for one of the derived classes, negating the advantage of inheritance.

    What you actually want to accomplish can be done with interfaces:

    interface IBase1 {
    }
    
    class Derived1 implements IBase1 {
      public void myMethod() {
      }
    }
    
    class Derived2 implements IBase1 {
    }
    
    class UseMe {
      public void foo(IBase1 something) {
         // Can take both Derived1 and Derived2
         // Can not call something.myMethod()
      }
      public void foo(Derived1 something) {
         something.myMethod();
      }
      public void foo(Derived2 something) {
        // not something.myMethod()
      }
    }
    

提交回复
热议问题