• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Thread question-no run() method signature

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
Code below compiled, executes and prints out nothing. My question is why b.run(); is OK when it was not overridden and class Bground does not implements Runnable. Thought it'll give compile error eg: if we call a method say b.test(); but did not define the method signature in the class eg void test(){};
Appreciate any help, thanks !!
Rob
From Marcus Green Mock : Question Bank Id:106, code added:b.test(); & comments
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
run() is a non-abstract method in Thread. Which is why u dont need to override the method in your class
Bijesh
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reason:
Thread is a non abstract class that implements Runnable and run() is a public abstract method in Runnable; so guess what, Thread is forced to implement run() method; if you want you can override it in your extended class;In that case your run() method would have been executed in a new thread instead of the blank run() method of the Thread class.
Sindhur
 
Rob Kiu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Bijesh, Sindhu,
Thanks for clearing that up. Look up the API to confirm, yes indeed Thread has a public void run method, whereas I thought only Runnable has the run method, so the code I quoted works!
From 1.4.2 API

run
public void run()
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.
Subclasses of Thread should override this method.


Cheers,
Rob
 
Did you just should on me? You should read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic