Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114


why does this class compile since there is no run method defined
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

rename the start method() to run()

hth


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
Threads is class that implements the Runnable interface....and since Thread isnt an abstract class run method must be implemented in it....so you can call run on Thread class but it does nothing


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114

no no i got it but its not what you said
actually we are extending thread class so its default run method is automatically inherited which does nothing ....
found this just now on a website
btw thanks
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

java.lang.Thread class implements Runnable interface and Runnable interface is having abstract run() method which has to be implement in every class which extends to Thread or implement Runnable.
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

oh forget my last post its not true , thread class it self implements run() method you don't need to implement run() in your class which extends to thread class.

but in case of runnable if you implement runnable interface you have to implement run() method.
Ben Power
Ranch Hand

Joined: Dec 30, 2009
Posts: 31
Minhaj kaimkhani wrote:...thread class it self implements run() method....

but in case of runnable if you implement runnable interface you have to implement run() method.


Yes. Thread does implement Runnable and it properly overrides the run() method, fulfilling the requirement to override run(). So when you extend Thread you inherit the perfectly good run() method in the Thread class. Note that while it fulfills the technical requirement to fulfill the run() method, it does not fulfill the practical need to create a usable run() method in your subclass. It compiles, but it's not useful.

On the other hand, if you create a class that implements Runnable, you need to override the run() method. Just in this case, you were technically inheriting Thread's run() method.

Ben


SCJP 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread
 
Similar Threads
could you pls explain this code and the output.
Marcus Exam
Thread question-no run() method signature
Marcus Green Mock # 2 Ques. 18, 33, 51
Extending Thread class