Today's Question:  What does your personal desk look like?        GIVE A SHOUT

Understanding abstract interface in Java

  sonic0002        2014-08-18 23:07:27       4,366        0    

Have you read about an interface declaration as public abstract interface InterfaceName in Java? At the first glance, is it a weird declaration? Why should we declare an interface as abstract? For example, we may see below code in some projects:

public abstract interface MyInterface {
    public void check();
    public abstract boolean check(boolean really);
}

Actually here abstract is redundant, an interface is implicitly abstract, we no need to put an abstract in front of interface. But it does no harm if you do.

You can find the below statement about abstract interface in JLS:

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

9.4 Abstract Method Declarations
For compatibility with older versions of the Java platform, it is permitted but discouraged, as a matter of style, to redundantly specify the abstract modifier for methods declared in interfaces.

From the JLS, we can find that this declaration is obsolete, this means it's not recommended to have this declaration in any future Java programs.

If we understand the meaning of abstract and interface, we can simply understand why abstract is not needed as well.

  • Both abstract and interface indicate the abstract class and interface cannot be instantiated, they need to be extended by other classes or interfaces.
  • Both abstract and interface indicate that other classes extend or implement the abstract class and interface need to implement the methods declared.

If this declaration is obsolete, is there any historic reason why this declaration exists? In Oak specification(The early Java), the interface declaration is similar to :

public interface Storing {
    void freezeDry(Stream s) = 0;
    void reconstitute(Stream s) = 0;
}

Note the "=0" after the method declaration. This is not required in the future version of Java. And in the specification it clearly states:

In the future, the " =0" part of declaring methods in interfaces may go away.

This indicates that "=0" is replaced with abstract and it seems it was mandatory at the early stage of Java. But now, with the improvement of JLS, these two keywords have more and more similarities. Hence abstract interface is not necessary any more.

JAVA  ABSTRACT INTERFACE 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  0 COMMENT


No comment for this article.