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
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.
run() is not abstract, as users need to override it, and why Thread.
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.
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 .
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 :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With