Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes run() method Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "run() method Doubt" Watch "run() method Doubt" New topic

run() method Doubt

Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
The following code compiles and runs successfully:

I was thinking as run() is not explicitly defined and implemented, it will give compiler error

Is the compiler intelligent enough to find that a run() method is available in the Thread class, so it is ignoring the implementation of Runnable interface?

Please help,

Jelle Klap

Joined: Mar 10, 2008
Posts: 1951

If you extend Thread you implicitly implement Runnable, because Thread IS-A Runnable. As Thread is not an abstract class it must provide an implemenation of the run() method, which it does; an empty one. Therefore you don't have to implement the run() method in your Thread subclass, even if you explicitly specify that it should implement the Runnable interface. It just doesn't make any sense not to override the run() implementation you inherit from the Thread class, because it does nothing and returns, which isn't terribly usefull.
[ March 27, 2008: Message edited by: Jelle Klap ]

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Thanks Jelle
I agree. Here's the link:
subject: run() method Doubt
It's not a secret anymore!