This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

run() method Doubt

 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,

Thanks,
Gitesh
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jelle
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic