Why declare an interface as abstract?

前端 未结 11 809
予麋鹿
予麋鹿 2020-12-13 03:36

What\'s point of declaring an interface as abstract? Same thing for an interface method. Is there a point to it?

eg.

public abstract interface Presen         


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

    have a look at this post

    https://stackoverflow.com/questions/4380796/what-is-public-abstract-interface-in-java/4381308#4381308

    interface is %100 abstract class.

    the keyword abstract is redundant here

    0 讨论(0)
  • 2020-12-13 04:31

    Where did you come across the chunk of code you have posted, any old java code base ?
    This is what the JLS has to say :

    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.

    0 讨论(0)
  • 2020-12-13 04:31

    "Why declare an interface as abstract?"-I got the same question and thought that abstract is redundant. But had to rethink when I saw the Map interface in java 1.8.May be this has to be changed in java

    //  (version 1.8 : 52.0, no super bit)
    // Signature: <K:Ljava/lang/Object;V:Ljava/lang/Object;>Ljava/lang/Object;
    public abstract interface java.util.Map {
    
      // Method descriptor #1 ()I
      public abstract int size();
    }
    
    0 讨论(0)
  • 2020-12-13 04:33

    The abstract modifier for an interface method is always redundant as well as the public modifier.

    The abstract modifier on the interface itself may be redundant for a strict technical reason as an interface can never be instantiated using the new operator and the interface will always be abstract if asked via reflection.

    However, there can be a semantic reason for declaring an interface abstract (that is also supported by various UML tools): You might want to express that an interface is explicitly declared abstract in the way that a non-abstract class may not implement the interface directly but only via a sub-interface. So e.g. you might consider the interface Node as semantically abstract while the sub-interfaces Folder and File that extend Node are semantically not abstract. You will never have an instance that is only a Node - it will be either a Folder or a File.

    Even further there are frameworks that allow "instantiation" of interfaces (technically via dynamic proxies). There some interface (e.g. a predefined base interface) are not allowed to supply as argument. For documentation purpose it can make sense in the source code to use the abstract modifier to express such information.

    0 讨论(0)
  • 2020-12-13 04:34

    The default behavior of an interface is essentially equivalent to what you have in your example. Defining it as abstract is just redundant.

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