Why doesn't Java allow private members in interface?

后端 未结 13 1329
时光取名叫无心
时光取名叫无心 2020-12-07 13:11

Why doesn\'t Java allow private members in interface? Is there any particular reason?

相关标签:
13条回答
  • 2020-12-07 13:23

    It is because they would be useless.

    There would be no way to call a private method.

    Private members are an implementation detail. An interface is about the public role that a class can take on.

    0 讨论(0)
  • 2020-12-07 13:24

    According to the Java programming language scope of the private members is limited to the class in which it is declared and can be accessed only by methods of that class. But inteface doesn't have a method body hence there is no use of declaring private members inside an interface.

    0 讨论(0)
  • 2020-12-07 13:26

    There would be no way to implement such an interface. An answer to a question I posed strongly suggests that it would be impossible (without radically changing the rules) to implement an interface with private methods - this leaves open the question of why protected and package private methods are not allowed.

    class OuterClass
    {
         void run ( MyInterface x )
         {
               x . publicMethod ( ) ;  // why not?
               x . protectedMethod ( ) ; // why not?
               x . packagePrivateMethod ( ) ; // why not?
               x . privateMethod ( ) ; // why not?
         }
    
         interface MyInterface
         {
               public abstract void publicMethod ( ) ; // OK
    
               protected abstract void protectedMethod ( ) ; // why not?
    
               abstract void packagePrivateMethod ( ) ; // in interface default is public, but why not package private
    
               private void privateMethod ( ) ; // impossible to implement
         }
    
         class MyImpl implements MyInterface
         {
               public void publicMethod ( ) { } // ok
    
               protected void protectedMethod ( ) { } // no sweat
    
               void packagePrivateMethod ( ) { } // no sweat
    
               private void privateMethod ( ) { } // not happening
         }
    }
    

    The below code should achieve the desired result. Even though all methods are public, only public method is effectively public. protected method is effectively protected. packagePrivateMethod is effectively packagePrivate. privateMethod is effectively private.

    class WorkAround
    {
         void run ( MyPrivateInterface x )
         {
               x . publicMethod ( ) ;  
               x . protectedMethod ( ) ; 
               x . packagePrivateMethod ( ) ; 
               x . privateMethod ( ) ; 
         }
    
         public interface MyPublicInterface { void publicMethod ( ) ; }
    
         protected interface MyProtectedInterface extends MyPublicInterface { void protectedMethod ( ) ; }
    
         interface MyPackagePrivateInterface extends MyProtectedInterface { void packagePrivateMethod ( ) ; }
    
         private interface MyPrivateInterface extends MyPackagePrivateInterface { void privateMethod ( ) ; }
    }
    
    0 讨论(0)
  • 2020-12-07 13:29

    Java allows private methods in an interface in Java 9. The default methods were introduced in Java 8. It is possible that multiple default methods want to share some code, then this code can be moved to a private method without exposing it to outer world. This bug has been fixed and starting in JDK 9 build 54, compiler support for private interface methods have been resurrected.

    public interface IData{
       default void processData(int data) {
          validate(data);
          // do some work with it
       }
       default void consumeData(int data) {
          validate(data);
          // do some work with it
       }
       private void validate(int data) {
         // validate data
       }
    }
    
    0 讨论(0)
  • 2020-12-07 13:29

    private fields would not be completely useless as other fields and inner classes could access them.

    However private methods could not be implemented, even in nested classes, making them almost useless. You could read them using reflection, but that is rather an edge case.

    0 讨论(0)
  • 2020-12-07 13:32

    As of Java 8, interfaces can have default methods, and as of Java 9, an interface is allowed to have a private methods which can only be accessed by default methods in the same interface.

    0 讨论(0)
提交回复
热议问题