Why declare an interface as abstract?

JavaOopInterface

Java Problem Overview


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 Presenter {
 public abstract void go(final HasWidgets container);
}

Java Solutions


Solution 1 - Java

Where did you come across the chunk of code you have posted, any old java code base ?
This is what the http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html">JLS</a> 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.

Solution 2 - Java

Interfaces and interface methods are implicitly abstract even if not declared as so. So there is no need to explicitly specify it.

Solution 3 - Java

Makes no difference - interfaces and interface methods are always abstract but you don't have to add the modifier (and interface methods are always public so you don't need the public modifier too).

From the JLS: > 9.1.1.1 abstract Interfaces > > Every interface is implicitly abstract. This > modifier is obsolete and should not be > used in new programs.

Solution 4 - Java

Typically, you don't declare the interface, or its methods, as abstract. They are implicitly.

The methods are also public, so you can skip that also. :-)

Solution 5 - Java

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

Solution 6 - Java

"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();
}

Solution 7 - Java

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

Solution 8 - Java

I think just verboseness, explicitness and consistency with the class syntax and semantics...

You don't have to, but maybe it could help if some reader of your code is distracted or not very versed in Java.

Solution 9 - Java

There is no point of declaring interface to be abstract. As the methods in the interface are abstract only.. One more thing abstract class can have both concrete and abstract methods but in the interface there should be only abstract methods.

Solution 10 - Java

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.

Solution 11 - Java

Question: Can we declare an Interface with “abstract” keyword? Answer: Yes, we can declare an interface with “abstract” keyword. But, there is no need to write like that. All interfaces in java are abstract by default. Same applies to interface methods

Please go throw this link https://javaconceptoftheday.com/java-interview-questions-on-interfaces/

INTERFACE CAN HAVE STATIC METHOD SINCE JAVA8

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSudhir JonathanView Question on Stackoverflow
Solution 1 - JavasateeshView Answer on Stackoverflow
Solution 2 - JavanotnoopView Answer on Stackoverflow
Solution 3 - JavaAndreas DolkView Answer on Stackoverflow
Solution 4 - JavaKLEView Answer on Stackoverflow
Solution 5 - Javauser467871View Answer on Stackoverflow
Solution 6 - JavaVasishta KurichetiView Answer on Stackoverflow
Solution 7 - JavaZack MarrapeseView Answer on Stackoverflow
Solution 8 - JavafortranView Answer on Stackoverflow
Solution 9 - JavagiriView Answer on Stackoverflow
Solution 10 - JavaJörgView Answer on Stackoverflow
Solution 11 - JavaZiaullhaq SavanurView Answer on Stackoverflow