Why declare an interface as abstract?

前端 未结 11 807
予麋鹿
予麋鹿 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: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.

提交回复
热议问题