• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Benefits of "extends Thread" vs "implements Runnable"

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic