The moose likes Beginning Java and the fly likes Overriding start() in Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overriding start() in Threads" Watch "Overriding start() in Threads" New topic

Overriding start() in Threads

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Overriding start() in Threads
When I override start method in a Thread created by „extends Thread
and a Thread created by „implements Runnable“ there is a different
reaction – WHY?

C:\>java BackgroundTest4a
In start()
 Thread is not started but start() method of Background object is invoked.

C:\>java BackgroundTest4
 Thread is started and not start() von Background is invoked!
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183
Because Runnable only defines one method: run.
So the call:
Thread thread = new Thread(runnable);;
Calls the default run() method in the Thread base class, which in turn calls the start() method of the Runnable instance runnable.
HTH :roll: :roll:
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
I just thought it over:
- "extends Thread" => bgr.start() is of type "Background" as bgr is background instance. According to Polymorphism: Whith overridden method the type of the object determines the invoked method: => bgr is of type "Background" hence "Background" start() method is invoked and not the "super.start" of Thread class!
- "implements Runnable" => new Thread(bgr).start() => Instance of Runnable => according to polymorphism => start() method of Runnable is invoked and Thread is startet.
I agree. Here's the link:
subject: Overriding start() in Threads
jQuery in Action, 3rd edition