1

I know the difference between all the access modifiers in Java. However, someone asked me a very interesting question that I struggled to find the answer to: What is the difference between a private interface and a public interface in Java, in particular, how it is used as a class member? Any help would be greatly appreciated.

3
  • 2
    It's analogous to the difference between a private and public class.
    – arshajii
    Sep 24, 2013 at 13:22
  • What does analogous mean? Sep 24, 2013 at 13:24
  • This link might be very useful.
    – Maroun
    Sep 24, 2013 at 13:27

3 Answers 3

2

I believe we all know the use of public interface, so I would mention the point of private/protected interface here.

Interfaces can be members of class definitions and can be declared private or protected there.

public class Test {  

    private interface Sortable {  
    }  

   protected interface Searchable {  
    }  

} 

Example 1: -- Source

public class PrivateInterface {  
     private interface InnerInterface {  
          void f();  
     }  

     private class InnerClass1 implements InnerInterface {  
           public void f() {   
               System.out.println("From InnerClass1");  
           }  
     }  

     private class InnerClass2 implements InnerInterface {  
           public void f() {   
               System.out.println("From InnerClass2");  
           }  
     }  

     public static void main(String[] args) {  
          PrivateInterface pi = new PrivateInterface();  
          pi.new InnerClass1().f();  
          pi.new InnerClass2().f();  
     }  
}   

/* Output: 
From InnerClass1 
From InnerClass2 
*/  

It's the interface itself that can be package-private, not the methods in it. You can define an interface that can only be used (by name) within the package it's defined in, but its methods are public like all interface methods. If a class implements that interface, the methods it defines must be public. The key thing here is that it's the interface type that isn't visible outside the package, not the methods.

1
  • +1 I learn something new everyday. Can't say I've ever thought of putting an interface as a class member.
    – Andrew Mao
    Sep 24, 2013 at 13:33
0

The public, private, and protected access modifiers on an interface mean the same thing that they mean on a class. I typically see these modifiers used on an interface that is nested in a class. Something like this:

//: interfaces/RandomWords.java  
// Implementing an interface to conform to a method.  
package interfaces;  

public class PrivateInterface {  
  private interface InnerInterface {  
        void f();  
  }  

  private class InnerClass1 implements InnerInterface {  
         public void f() {   
             System.out.println("From InnerClass1");  
         }  
  }  

  private class InnerClass2 implements InnerInterface {  
         public void f() {   
             System.out.println("From InnerClass2");  
         }  
  }  

  public static void main(String[] args) {  
        PrivateInterface pi = new PrivateInterface();  
        pi.new InnerClass1().f();  
        pi.new InnerClass2().f();  
  }  
}  
0

An interface declaration may include these access modifiers:

public protected private abstract static strictfp

public: If an interface type is declared public,then it can be accessed by any code.

protected/private: The access modifiers protected and private pertain only to member interfaces within a directly enclosing class declaration. A member interface is an interface whose declaration is directly enclosed in another class or interface declaration.

static: The access modifier static pertains only to member interfaces, not to top level interfaces.

abstract: Every interface is implicitly abstract. This modifier is obsolete and should not be used in new programs.

strictfp: The effect of the strictfp modifier is to make all float or double expressions within the interface declaration be explicitly FP-strict.

Ref: Java Language and Virtual Machine Specifications

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.