Why run the method defined using the abstract keyword in the runnable interface

This question may be silly, but I accidentally checked in the Java source code that runs the method in the runnable interface using an abstract keyword. But according to the definition of the interface, all methods in the interface are abstract by default. Then I am confused why the specially Runnable interface has an abstract keyword for the run method. I check other interfaces like map, list, etc., but someone doesn't have an abstract keyword.

Please let me know why this is written in the Java source code.

public abstract void run(); 

thanks

+6
java
source share
5 answers

From the java language specification :

Each method declaration in the body of the interface is implicitly abstract, therefore its body is always represented by a semicolon, not a block.

Each method declaration in the body of the interface is implicitly public.

For compatibility with older versions of the Java platform, it is allowed but discouraged, as a matter of style, to specify the modifier abstract for methods declared in the interfaces redundantly.

It is allowed, but very discouraged, as a matter of style, to redundantly indicate the public modifier for interface methods.

Hey, just found out that my interface declarations are bad style because I always use the public modifier.


apache Harmony adds a public modifier (uuuh - bad style!). SUN Does Oracle have both modifiers? I assume that since in the “older versions” it was mandatory to add the abstract modifier only because JLS mentions “compatibility with older versions”.

And then: never modify the Runnable system :-)

+8
source share

The public declaration is also redundant, since all characters are publicly accessible by default in the interface.

This is probably out of habit. I believe Runnable was one of the first few interfaces conceived in JDK 1.0, and this time, the default interface declarations for the interfaces probably were not fully embedded in the minds of JDK developers. I remember reading in an interview with James Gosling that in Oak, the name of the project for what became Java, there used to be interfaces, but only abstract classes like C ++, and it could be a hangover from that.

I also sometimes write "public" for interface methods and constants, although this is not necessary.

+14
source share

All methods in the interface are public and abstract, but in fact you do not need to indicate that the use of keywords. This will be done automatically for you. You can only have void run() , and that will mean the same thing.

+1
source share

The reason it was written in the Java source code is because the author missed it.

Definition:

 public abstract void run(); 

exactly matches the definition:

 public void run(); 

in the interface. It is just a matter of style. This is the preferred style in Java interfaces, which is determined by interface methods without public and abstract keywords.

Note that there is no difference in functionality between the two method definitions in the interface.

0
source share

This seems like a pretty pointless question. At least this is a pointless place to ask about it. You will need to ask the author. To which he will probably answer "who cares?"

0
source share

All Articles