Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread question-no run() method signature

 
Rob Kiu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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
 
Bijesh Krishnadas
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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
 
Sindhur Sat
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic