[ACCEPTED]-Why declare an interface as abstract?-interface
Where did you come across the chunk of code 9 you have posted, any old java code base 8 ?
This is what the JLS has to say :
9.1.1.1 abstract Interfaces:
Every 7 interface is implicitly abstract. This modifier 6 is obsolete and should not
be used in new 5 programs.
9.4 Abstract Method Declarations:
For compatibility with older versions 4 of the Java platform, it is permitted but
discouraged, as 3 a matter of style, to redundantly specify 2 the abstract modifier
for methods declared 1 in interfaces.
Interfaces and interface methods are implicitly 2 abstract
even if not declared as so. So there is 1 no need to explicitly specify it.
Makes no difference - interfaces and interface 7 methods are always abstract but you don't 6 have to add the modifier (and interface 5 methods are always public so you don't need 4 the public modifier too).
From the JLS:
9.1.1.1 3 abstract Interfaces
Every interface is implicitly 2 abstract. This modifier is obsolete and 1 should not be used in new programs.
Typically, you don't declare the interface, or 3 its methods, as abstract. They are implicitly.
The 2 methods are also public, so you can skip 1 that also. :-)
have a look at this post
interface is %100 2 abstract class.
the keyword abstract is redundant 1 here
"Why declare an interface as abstract?"-I 4 got the same question and thought that abstract 3 is redundant. But had to rethink when I 2 saw the Map interface in java 1.8.May be 1 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();
}
The default behavior of an interface is 3 essentially equivalent to what you have 2 in your example. Defining it as abstract 1 is just redundant.
I think just verboseness, explicitness and 4 consistency with the class syntax and semantics...
You 3 don't have to, but maybe it could help if 2 some reader of your code is distracted or 1 not very versed in Java.
There is no point of declaring interface 5 to be abstract. As the methods in the interface 4 are abstract only.. One more thing abstract 3 class can have both concrete and abstract 2 methods but in the interface there should 1 be only abstract methods.
The abstract modifier for an interface method 26 is always redundant as well as the public 25 modifier.
The abstract modifier on the interface 24 itself may be redundant for a strict technical 23 reason as an interface can never be instantiated 22 using the new operator and the interface 21 will always be abstract if asked via reflection.
However, there 20 can be a semantic reason for declaring an 19 interface abstract (that is also supported 18 by various UML tools): You might want to 17 express that an interface is explicitly 16 declared abstract in the way that a non-abstract 15 class may not implement the interface directly 14 but only via a sub-interface. So e.g. you 13 might consider the interface Node as semantically 12 abstract while the sub-interfaces Folder 11 and File that extend Node are semantically 10 not abstract. You will never have an instance 9 that is only a Node - it will be either 8 a Folder or a File.
Even further there are 7 frameworks that allow "instantiation" of 6 interfaces (technically via dynamic proxies). There 5 some interface (e.g. a predefined base interface) are 4 not allowed to supply as argument. For documentation 3 purpose it can make sense in the source 2 code to use the abstract modifier to express 1 such information.
Question: Can we declare an Interface with 7 “abstract” keyword? Answer: Yes, we can 6 declare an interface with “abstract” keyword. But, there 5 is no need to write like that. All interfaces 4 in java are abstract by default. Same applies 3 to interface methods
Please go throw this 2 link https://javaconceptoftheday.com/java-interview-questions-on-interfaces/
INTERFACE CAN HAVE STATIC METHOD SINCE 1 JAVA8
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.