• Post Reply Bookmark Topic Watch Topic
  • New Topic

Benefits of "extends Thread" vs "implements Runnable"  RSS feed

 
landon manning
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the benefit of having a class extend Thread vs having a class implement Runnable?
<code>
public class T extends Thread{

public static void main( String[] args ){

//What is the benefit of doing this...
new T().start();

//...as opposed to this. (Or vice versus)
new Thread( new R() ).start();
}

public void run(){
System.out.println("In T");
}

}
class R implements Runnable{

public void run(){
System.out.println("In R");
}
}
</code>
Thanks,
Landon Manning
 
vishad patel
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,friend
As you kbnow that java does not support multiple inheritance so that when you can write coding for applet then you extends the applet class ,in such a case you can't extends the Thread.so that to avoid this kinds of problem you have to implements the Runnable.
I think this is sufficient for your understanding.
Thanks .
if i am wrong then mail me.
vishad20002001@yahoo.com
Bye.
 
Jerry Pulley
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Landon,
The only good reason to derive a subclass from Thread is to extend its behavior - just like any other class. It's usually a better idea to implement Runnable.
Delegation, when it satisfies the requirements, is a more flexible solution than specialization. While it's true that overriding run() is in some sense extending Thread's behavior, it's more in keeping with good OO practice to derive a Thread subclass only if you need to modify some more basic aspect. In the simple example programs you see in most books (where the author often just makes the main class a subclass of Thread), the two approaches are equivalent. In larger programs, this kind of failure to make the code reflect the design abstractions often leads to trouble.
Just my opinion, of course.
Jerry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!