Reduce visibility when implementing interface in Java

后端 未结 4 1344
误落风尘
误落风尘 2021-01-11 18:12

I would like to design class A implements interface C and reduce the visibility of a method (declared in C)to make it secure from outer world, make one of the methods in in

4条回答
  •  伪装坚强ぢ
    2021-01-11 18:27

    This approach worked for me. Any new function added to PrivateInterface would break still break PublicSampleClass

    private interface PrivateInterface {
        void fooBar();
    }
    
    public class PublicSampleClass {
    
        private final listenerInterface = new PrivateInterface {
             public void fooBar() {
                PublicSampleClass.this.fooBar();
             }
        };
    
        protected void fooBar() {
          // Non public implementation
        }
    
    }
    

提交回复
热议问题