0

Possible Duplicate:
“implements Runnable” vs. “extends Thread”

Can anybody Explain when we extend a Thread class and when we implement runnable interface in java. When we have to use those concepts? I'm feeling little bit tricky for finding the exact answer.

Community
  • 1
  • 1
vivek
  • 111
  • 1
  • 12

2 Answers2

6

It is always recommended to implement Runnable rather than extend Thread as implementing Runnable would force you to implement run() method. extending Thread wouldn't force you as Thread class it self implements run method. also as java doesn't support multiple inheritence using class's when your class is already extending another class and you need Threading in your class implementing Runnable is the way to go in such scenarios

PermGenError
  • 45,977
  • 8
  • 87
  • 106
1

From Java Tutorial:

Which of these idioms should you use? The first idiom, which employs a Runnable object, is more general, because the Runnable object can subclass a class other than Thread. The second idiom is easier to use in simple applications, but is limited by the fact that your task class must be a descendant of Thread.

Using Runnable is usually recommended as if you subclass Thread, you can't inherit from multiple classes in Java, so you're restricted that way.

Swapnil
  • 8,201
  • 4
  • 38
  • 57