Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why run method defined with abstract keyword in runnable interface

Tags:

java

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

like image 751
viren Avatar asked Jul 20 '10 12:07

viren


People also ask

Which method in runnable interface is abstract?

The Runnable interface is a functional interface defined in java. lang package. This interface contains a single abstract method, run() with no arguments. When an object of a class implementing this interface used to create a thread, then run() method has invoked in a thread that executes separately.

Is run method is abstract in thread class?

run() is not abstract, as users need to override it, and why Thread.

Why do we declare abstract methods?

The abstract methods merely define a contract that derived classes must implement. It's is the way how you ensure that they actually always will.

Is an interface with defines the run () method?

Interface Runnable The class must define a method of no arguments called run . This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread .


1 Answers

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 :-)

like image 186
Andreas Dolk Avatar answered Oct 21 '22 05:10

Andreas Dolk