Tutorial :Why run method defined with abstract keyword in runnable interface



Question:

This question may be silly but i accidentally checked in java source code that run method in runnable interface is defined with abstract keyword. But according to interface definition all methods in an interface are by default abstract. Then i am confused why specially Runnable interface has abstract keyword for run method. I check with other interfaces like map , list etc but no one has abstract keyword.

Please give me an idea why it is written like this in java source code.

public abstract void run();  

Thanks


Solution:1

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.

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.

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


apache Harmony just adds the public modifier (uuuh - bad style!). SUNOracle has both modifiers? I guess that's because in the 'older versions' it was mandatory to add the abstract modifier - just because the JLS mentions 'compatibilty with older versions'.

And then: Never change a Runnable system :-)


Solution:2

The 'public' declaration is also redundant, since all symbols are public by default in an interface.

It's probably written like this just out of habit. I imagine that Runnable was amongst the first handful of interfaces conceived in JDK 1.0, and that that time, the declaration defaults for interfaces had probably not been fully instilled in the minds of the JDK developers. I remember reading in an interview with James Gosling, that in Oak, the project name for what became Java, there were once no interfaces, just abstract classes like C++, and this may be a hangover from that.

I also sometimes write "public" for interface methods and constants, although it's not necessary.


Solution:3

All methods in an interface are public and abstract, but you are not actually required to specify that using keywords. It will be done automatically for you. You could just have void run() and it would mean the same thing.


Solution:4

The reason it's been written like this in the Java source code is because it has been overlooked by the author.

The definition:

public abstract void run();  

is exactly the same as the definition:

public void run();  

in an interface. It is simply a matter of style. It is the preferred style in Java interfaces that interface methods be defined without the public and abstract keywords.

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


Solution:5

This seems like a pretty pointless question. At least it's a pointless place to ask it. You would need to ask the author. To which he would probably reply 'who cares?'


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »