This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
In most cases you would implement the runnable interface. To extend from the Thread class means you cannot extend from any other classes, so if your design changes ... good luck Also, by extending Thread when all you really want is for the code to run in a separate thread is excessive. You are inheriting all the members of the Thread class. Ask yourself, do you really need them? In summary, implement Runnable except in those rare cases you need the extra functionality of the Thread class.
There is at least one instance when you must implement runnable instead of inheriting the thread class. When you are witing an applet inheriting the applet class ie MyClass extends Applet, you can't say Myclass extends Applet extends Thread since JAVA do not support multiple inheritence. You must implement the runnable interface in this case. ie MyClass extends Applet implements Runnable. Kalidas
Joined: Nov 24, 2000
Jim, Gurpreet, Kalidas, Thanks for responding to this thread. Gurpreet,you said that extending THREAD class results in inheriting all the class under it. Does this in any way reduce the performance by putting extra overhead during runtime or anything of that sort. Thanks yet again. -Rajendra.
Joined: Jan 07, 2001
Joined: Jan 05, 2001
Actually, I really didn't say that correctly. If something IS A Thread then it should subclass Thread and not just implement Runnable unless there is some MI issue. E.G. a TimerThread class IS A Thread so should subclass Thread unless some mult. inher. restriction prevents it. This is just OO design practice. The performance effects are negligible - you don't inherit any other subclasses of Thread just because you inherit from Thread.
Originally posted by Rajendra Deshpande: Jim, Gurpreet, Kalidas, Thanks for responding to this thread. Gurpreet,you said that extending THREAD class results in inheriting all the class under it. Does this in any way reduce the performance by putting extra overhead during runtime or anything of that sort. Thanks yet again. -Rajendra.