Can anyone tell me difference between specifying interface public and abstract?
public interface Test{}
and
abstract interface Test{}
Can anyone tell me difference between specifying interface public and abstract?
public interface Test{}
and
abstract interface Test{}
The former is an interface that can be accessed from anywhere. The latter (since abstract is superfluous) is an interface that can be accessed within the same package, as it has default access modifiers.
Interfaces are implicilty abstract. So abstract interface
is not needed.
From JLS : http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.1.1.1
9.1.1.1. abstract Interfaces
Every interface is implicitly abstract.
This modifier is obsolete and should not be used in new programs.
It is redundant to declare an interface as abstract
as they are all abstract
by definition. The only difference, then, is the public
component. Your first interface (which is public) can be accessed from anywhere whereas your second (which has default visibility) is package-private, meaning it can only be accessed within its own package.
public abstract interface
means it will be available in all the packages. But abstract interface
will only be available in the current package because of the default access. Read more about access specifiers here:
http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
And packages are abstract by nature, which means they cannot be isntantiated. And all the methods in the package will be abstract. Having the purely abstract method, mandates an implementing class to override them.
As always, the JLS provides the answer (JLS 9.1.1.1):
Every interface is implicitly abstract.
Of course, I'm assuming that you mean to include the public
modifier on your second example. If you don't, then there are other differences between the two -- but they do not relate to the abstract
keyword.