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 Answers
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 {
}
}
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 I learn something new everyday. Can't say I've ever thought of putting an
interface
as a class member. Sep 24, 2013 at 13:33
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();
}
}
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.
private
andpublic
class.