Is there a point of marking a method in interface abstract? [duplicate] - java

This question already has answers here:
Why would one declare a Java interface method as abstract?
(4 answers)
Closed 7 years ago.
I've refactored some methods to an interface and they were left with abstract modifier in the interface declaration. Something like this was in my code:
public interface TestAbstractMethod {
public abstract void doSomething();
}
I noticed it accidentally now and I was actually surprised that the declaration wasn't marked as invalid, that it actually compiles and works.
Is abstract modifier allowed in an interface for a reason? Does this alter the interface's behavior in any way?

Yes, this is allowed, but its use is discouraged in the Java Language Specification, §9.4:
Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.
It is permitted, but discouraged as a matter of style, to redundantly specify the public and/or abstract modifier for a method declared in an interface.

Related

Difference between using an abstract keyword for a method in interface vs not using the abstract keyword [duplicate]

This question already has answers here:
Why would one declare a Java interface method as abstract?
(4 answers)
Closed 6 years ago.
Say I have a Interface FirstInterface as follows :
public interface FirstInterface {
public void myInterfaceMethod();
}
Here I am declaring a method myInterfaceMethod() which will be defined in the class that will implement this Interface.
but I can also do
public interface FirstInterface {
public abstract void myInterfaceMethod();
}
I have added a keyword abstract in the method declaration.
I want to know, if at all, does it make any difference to add a abstract keyword in the method declaration or not?
All non-default, non-static methods in interfaces are abstract by default. Adding the keyword is harmless, but changes nothing and is discouraged by the JLS "as a matter of style."
From JLS§9.4:
An interface method lacking a default modifier or a static modifier is implicitly abstract... It is permitted, but discouraged as a matter of style, to redundantly specify the abstract modifier for such a method declaration.

Are "public" and "public final" redundant for interface fields?

I was reading this post Why would a static nested interface be used in Java? in particular the first answer. In that answer is written that use the words "public" or "public final" on interface fields are redundant. My question is: why?
Why should I remove them? If I have something like this:
public interface Int1 {
public void add();
void remove();
}
Doesn't it mean that I want add method to be implementated by whatever class while remove method to be implementated only by classes of my same package?
Are “public” and “public final” redundant for interface methods?
Yes.
All methods in an interface are implicitly public and abstract (but not final).
All fields in an interface are implicitly public, static and final.
The JLS states this. It also states that these modifiers can be left out.
Why? Well there are a variety of reasons:
Fields and methods are implicitly public because the point of an interface is to declare an ... interface that other classes can see. (If you want / need to restrict access, this is done via an access modifier on the interface itself.)
Fields are static because if they were not you would be declaring visible instance fields on an object ... and that's bad for encapsulation.
Fields are final because non-final fields would be another way of declaring public static fields ... which are terrible from an OO perspective.
Methods are abstract because allowing method bodies would effectively turn interfaces into abstract classes.
Another reason for making methods abstract and fields static in an interface is that if they didn't, diamond inheritance, and inheritance of a method from two distinct interfaces would both be problematic.
But either way, this is how Java is defined, so the questions are moot ... unless you are thinking of inventing your own programming language.
Note that in Java 8, you can declare methods in an interface, using the default modifier. And in Java 9, you can declare private methods, in some cases. But use of the public keyword is still redundant.
Why should I remove them?
You don't have to remove them. The Java compiler doesn't care.
You can remove them, but you don't have to remove them, unless you are trying to conform to some Java style guidelines that insist on this.
Your code will probably be more readable if you are consistent, but you could make it consistent by using the redundant modifiers everywhere; e.g. adding them rather than removing them.
Doesn't it mean that I want add method be implemented by whatever class while remove method implemented only by classes of my same package?
No it doesn't mean that. Or at least, it might mean that to you, but it won't mean that to the Java compiler, other Java tools ... or other people reading and maintaining your code. IMO, it would be ill-advised to place any meaning on the presence or absence of redundant keywords.
You cannot have a final method declared in an interface. Fields are always final but methods are always abstract (and never final). You cannot define an interface method that is to be implemented only by classes in the same package.* From section 9.3 of the Java Language Specification:
Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields.
and from section 9.4:
Every method declaration in the body of an interface is implicitly public (§6.6).
Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.
It is permitted, but discouraged as a matter of style, to redundantly specify the public and/or abstract modifier for a method declared in an interface.
* As Paul Bellora points out in a comment, you can make the interface itself package-private (or protected, or even private) if you want to restrict its visibility.
Interfaces by definition are abstract so the abstract modifier on the interface is redundant.
Variables in interfaces and annotations are automatically public, static and final, so these modifiers are redundant as well.
As annotations are a form of interface, their fields are also automatically public, static and final just as their annotation fields are automatically public and abstract.
Final classes by definition cannot be extended so the final modifier on the method of a final class is redundant.
reading this: http://checkstyle.sourceforge.net/config_modifier.html
Yes the public is redundant, because in an Interface all methods are implictly public and abstract.
I think its is a bad style to add public, or abstract, because both are implicitly applied.
public interface Int1 {
void add();
void remove();
}
This looks cleaner, and shows that you know, that they are implict public
from Java Language Specification (JLS)
9.4. Abstract Method Declarations
Every method declaration in the body of an interface is implicitly public (§6.6).
Every method declaration in the body of an interface is implicitly
abstract, so its body is always represented by a semicolon, not a
block.
It is permitted, but discouraged as a matter of style, to redundantly
specify the public and/or abstract modifier for a method declared in
an interface.
I write interfaces without the public keyword for methods. It's redundant.

what is a abstract method on a interface in java [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why would one declare a Java interface method as abstract?
I found the following code in one of our ejb interfaces. Does anyone know what the abstract does in the interface? If you do please also explain why it might be needed or provide a reference to read about it =)
#Local
public interface IDomasOrderProcessor {
public abstract void executeOrderLines(List<OrderLine> lines);
public abstract void setupJob(List<OrderLine> lines);
public abstract void setupJob(OrderLine line);
}
abstract is redundant in this case. All methods defined on an interface are public and abstract by definition.
Excerpt Java Language Specification section 9.4
Every method declaration in the body of an interface is implicitly
abstract, so its body is always represented by a semicolon, not a
block.
Every method declaration in the body of an interface is implicitly
public.
Both public and abstract modifiers are implicit in interfaces and should be avoided.
A method in an interface is public and abstract by definition. I have heard some people say they feel that explicitly declaring them like that makes it clearer, but to me it seems like extra noise.
As per this
document all the methods of interface is public and abstract, so there is no mean to define explicitly abstract method inside the interface.

Are there any legitimate reasons to hide static methods? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why doesn't Java allow overriding of static methods?
Is there any legitimate reason why one would want a derived class to override hide a static method of the base class?
Terminology aside, static methods in Java do have a kind of overriding relation, implied by binary compatibility section 13.4.12. If T extends S, S declared m(), T.m() can refer to a method in T or S, depending on if m() is declared in T; and it's ok to add or remove m() from T, without breaking any code calling T.m(). (This implies JVM invokestatic instruction does a sort of dynamic method lookup up the super class chain)
However, this is nothing but trouble. It is really dangerous if the meaning of T.m() silently changes because now it's pointing to a different method. (Instance methods shall inherit contracts so that's not a problem; there's no such understanding in static methods.)
So this "feature" should never be used; the language shouldn't have enabled it to begin with.
The good practice: If we call T.m(), m() must be declared in T; and it should never be removed from T without removing all T.m() first.
Static methods cannot be overriden
In order to override a method, the method must first be inherited. If the method is not inherited there is no chance for overriding. Therefore, you can never override a private method as they are not inherited.

What is the difference specifying abstract or not for interface methods?

What is the difference between specifying the abstract keyword on a method of an interface in Java, and not specifying it?
Like:
public void foo();
public abstract void foo();
There is no difference. See the JLS Interfaces - Abstract Method Declatations:
Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.
Also note:
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.
there is no difference all methods in interfaces are implicit abstract because to implement that interface all methods must be overriden ...
strange that it´s working however

Categories

Resources