This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
What is difference between Implementing Runnable & extending Thread class.
one I know,
Java not support multiple Inheritance so If our class is extending any other class and we need to extend that class from Thread class,then it is not possible in this case we are Implementing Runnable interface.
correct me If I am wrong.
Is there any other difference between Implementing Runnable & extending Thread class?
Java not support multiple Inheritance so If our class is extending any other class and we need to extend that class from Thread class,then it is not possible in this case we are Implementing Runnable interface
Yes. That's a an important scenario for using Runnable over extending Thread. And By using Runnable it's easy to seperate the logic to be run in a seperate thread from the thread itself.
Because you are not messing the runnable code (which is inside the Runnable implementation) with the Thread which actually runs the code. So the logic is seperated from other code,easy to change and manage.
Ok. Here is the Scenario, Suppose your class extends Thread class and does not provide run() method the compiler or the runtime will not give any errors or warnings and the bytecode runs successfully. Whereas, if you implements Runnable interface which is always the best choice (recomended) you get an compile-time error something saying your class does not override run() method or simply declare your class as abstract.
The most important thing to note here
if you inherit from a class you are doing specialization of the parent implementation, whereas, if you inherit from an interface you have to provide the implementation ourself of all the method specified in that interface.