• Post Reply Bookmark Topic Watch Topic
  • New Topic

extends or implements?

 
siva prasaad
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When to use extends Thread Class
and
When to use implements Runnable Interface.

Regards
Siva Prasad
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to extend the functionality of Thread then use extends (which in most cases you do not want).

Then what usually a programmer wants ?

You want to create a job that would run in a different thread and for that create the job by implementing Runnable interface and let a thread run it.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to create a job that would run in a different thread and for that create the job by implementing Runnable interface and let a thread run it.


Hey i am a bit confused!!
i did not follow this statemant please ellaborate and explain this.
Thanks in advance.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly you want to do ?

Usually we want to do certain stuff / job with a separate stack of execution.

In that case you just need to define the job that you want to run as a different thread as a class implementing Runnable interface(and offcourse implementing run method).In this case you an logically not extending functionality of thread , rather defining a unit of work that you want to execute as separate thread.

Hope this helps,
[ May 28, 2007: Message edited by: Rahul Bhattacharjee ]
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
for such a good explanation.
 
Sam Semwal
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should ideally be using implements as this is the preffered way although a bit more code but it is worth. In case of extends you can not extend any more class.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is in fact an FAQ: ExtendingThreadVsImplementingRunnable
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!