I have the following interface in Java
public interface IFoo
{
public abstract void foo();
public void bar();
}
What is the difference between foo() and bar()?
When should I use abstract?
Both seem to accomplish what I want unless I'm missing something subtle?
Update Duplicate of Why would one declare a Java interface method as abstract?
There isn't any functional difference. No implementation is ever provided in a java interface so all method declarations are implicitly abstract.
See [1]: http://java.sun.com/docs/books/tutorial/java/IandI/abstract.html
A direct quote form the above:
Note: All of the methods in an interface (see the Interfaces section) are implicitly abstract, so the abstract modifier is not used with interface methods (it could be—it's just not necessary).
Interface methods are both public and abstract by default. There's no difference between foo() and bar() and you can safely remove all public and abstract keywords.
You're not missing anything. From the Java Language Specification:
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.
In other words, you can omit the public as well as the abstract on the interface methods.
It's redundant (there's no difference between the two declarations) and explicitly discouraged in section 9.4 of the Java Language specification:
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.
And public is similarly unnecessary and discouraged:
It is permitted, but strongly discouraged as a matter of style, to redundantly specify the
public modifier for interface methods.
I don't know why public is strongly discouraged but abstract is just discouraged...
Both accomplish the same, since all methods in an interface are abstract.
abstract in this scenario is unnecessary (as is marking methods in interfaces as public).
You would instead use this on a method in an abstract class in order to enforce its overriding by a subclass.
There no are difference between declarations. Conceptually, all methods in an interface are abstract.
It makes no difference. All the methods in a java interface are always abstract.
See the first note at this link :
http://java.sun.com/docs/books/tutorial/java/IandI/abstract.html
Both foo() and bar() are abstract as they are declared inside an interface. The abstract word you used here is of no significance - you better remove it.
Interfaces in java are equal "somehow" to a fully abstract classes, so adding the "abstract" keyword to a method declaration in an interface has nothing to do with the method!
Related
I've been wondering why it's allowed to do a code implementation in an interface, when interfaces are suppossed to contain no code implementation:
public interface someInterface{
String someString = "example";
}
I can make a class implement this interface, without getting an error:
public class someClass implements someInterface
How come?
You are allowed to declare constants in interfaces, which is what you have done. You have not implemented code.
Variables declared in interfaces are implicitly declared public static final.
The JLS, Section 9.3, covers this:
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.
According to java docs
Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
Here you are not defined any methods to implement.So you didn't get any error here.
There is no strict condition that an interface must have signatured methods.Remember there are Marker Interfaces too in java.
And secondly , You can declare variables inside interface.
And that variable someString assigned in a static context and shared across all the implemntations by that interface
Point is that the variables inside declared interface are implicitly static and final.You can use them.
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.
I'm reading SCJP by katherine sierra.
I understand that abstract and final keywords cannot be used together because they contradict each other as explained in the book.
However, I don't understand why strictfp and abstract keywords cannot be used together.
I don't know how the strictfp keyword exactly works in Java yet.
In my thoughts, one could declare an abstract strictfp method, have a subclass, and implement that method in the "strictfp way".
What is the reason these keywords don't get along well together ?
EDIT
I've double checked the book and it surely says
Because interface methods are abstract, they cannot be marked final,
strictfp , or native .
from SCJP by Katherine Sierra. page 21.
Also my IDE(Eclipse Juno) says I can't use abstract and strictfp keywords together.
Hmmm, why not though ?
Katherine Sierra was probably talking about abstract methods. It would make no sense to make an abstract method strictfp because an abstract method just provides a method signature (and throws clause if any), to use it we need to override it in a subclass with a concrete method and this method will have its own modifiers which will override its parent method's modifiers. That is, methods do not inherit modifiers.
Note that it's not only sctriptfp, but no modifiers are allowed on abstract methods except public and protected. You'll get a compile-time error if you try.
Hope you already got your answer by now if not then it may be useful for you.
I have encountered a similar question asked in a forum.
The reason why abstract and strictfp can't sit together in the method declaration is because abstract says the method must not be implemented by the current class and it must be implemented by the concrete subclass and strictfp says the method should be implemented (should have a body) by the class where strictfp is used. So in this case both key words contradict with each other hence both are not allowed together in method declarations.
But it is absolutely legal to use abstract and strictfp before class. Something like
public abstract strictfp class MyAbstractClass{} //is ok
If you declare strictfp in a abstract class then all its method is going to be strictfp by default. Remember all the concrete methods in side the class not the abstract methods.
Run the example given below and see the OP:
import java.lang.reflect.*;
public abstract strictfp class AbstractStrictfp
{
public abstract void abstractMethod();
public void concreteMethod(){};
public static void main(String args[]){
Method methods[] = AbstractStrictfp.class.getMethods();
for(Method method : methods){
System.out.println("Method Name::"+method.getName());
System.out.println("Modifiers::"+Modifier.toString(method.getModifiers()));
System.out.println();
}
}
}
As you know, in a java interface, all methods have to be defined as abstract. But when I define a method as not typing abstract, the compiler says it is okay. I know that an abstract method must not have a body. Does a method somewhere in an interface necessarily have a name abstract or not? : What i mean is, what is the difference between:
public interface blabla {
public void aMethod();
//or
public abstract void aMethod();
}
No, marking an interface method as abstract has no meaning and is never required.
All interface methods are implicitly abstract (and public too btw).
From the JLS:
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.
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.
It is permitted, but strongly discouraged as a matter of style, to redundantly specify the public modifier for interface methods.
Related question (+ answer with a historical reference to a statement saying that abstract was once required for interface methods):
Java abstract interface
See the sample example below
interface xyz
{
void methodA();
}
Save this to xyz.java
Now compile this using javac tool
and then use the command given belo
javap xyz
the output would be
Compiled from "xyz.java"
interface xyz {
public abstract void methodA();
}
That means when you compile an interface, compiler makes its signature to public and abstract by default.
So it is not necessary to use abstract keyword for any method of interface.
I don't know that they have to be defined as abstract. Probably because they don't. See Oracle's tutorial.
you don't need to specify abstract (default) because within an interface it does not make sense as all the method of the interface needs to be implemented
All methods in an interface are abstract by definition.
You can't create an object out of an interface (e.g., using Interface i = new Interface();) so there's no difference between marking a method as abstract or not.
Any class that implements the interface needs to decide whether to implement it or to let a subclass do it. So as far as the interface is concerned, all methods are abstract by default.
An abstract method provides no implementation. A class which has an abstract method is necessarily abstract, which means that you cannot create instances of this class. To create an instance of that class, you need to subclass and provide non-abstract overwrites for the abstract methods.
An interface never provides an implementation of its methods and it cannot be instantiated. Therefore every method of an interface is per definition abstract. You do not need to provide the keyword abstract when declaring a method in an interface. And by convention the keyword abstract is not used within an interface.
The methods of an interface don't have to be explicitly defined as abstract because they are implicitly abstract and public as defined in the Java Language Specification §9.4. A redundant declaration is perfectly legal though.
If you forgot to put abstract keyword before interface method, Java will implicitly put public abstract keyword before it. Because all interface methods must be abstract.
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