wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Extending Thread class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extending Thread class" Watch "Extending Thread class" New topic
Author

Extending Thread class

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 371
Thread class implements Runnable interface. So when we wxtend Thread class Run() must be defined.
Following code sample is taken from mocktest in another section. In this code, run() is not implemented. Is not that a compile time error ? When I read the answer, it says code will run fine, but will not print anything.
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 142

Thread implements run(), but does nothing.
When extending classes, you don't have to implement their methods. You may overwrite them, but you don't have to. So a call to the run() method of a Bground object will call Thread's implementation of run(), which simply does nothing.


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Jan Hoppmann wrote:Thread implements run(), but does nothing.

Actually it does something. If you create a Thread with a Runnable as argument to the constructor, it will call run() on that Runnable. If you don't provide a Runnable then it does nothing. Part of the source code for Thread, taken from src.zip:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

You have to tell what the method run does.

Take the code you wrote in the start method and put it in the run() method.



[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Michel ten Voorde
Greenhorn

Joined: Dec 30, 2010
Posts: 26

I think nirjari's question is whether extending the Thread class implies having to implement the run() method. This is not the case, your extended class inherits the run() method which is already in the Thread class. I think you confuse the 2 ways of defining a thread:

1) Extend the Thread class and override the run() method.
2) Implement the Runnable interface and implement the run() method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extending Thread class
 
Similar Threads
Threads: start() and run() methods
could you pls explain this code and the output.
Thread
Marcus Green Mock # 2 Ques. 18, 33, 51
Marcus Exam