• Post Reply Bookmark Topic Watch Topic
  • New Topic

Runnable

 
Robert Kennedy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,


I have a method which implements runnable. The runnable connects to a server and sends some data. I would like the runnable to be able to accept as a parameter any object - which should have a method: void setResponse(boolean b)

The runnable will then feedback the server response to the object.
Do i use an interface?

Thanks
 
Santhosh Kumar
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depending on how generic the Runnable class is and where all it would be used. Nevertheless the preference is with interfaces.
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your class (not method) that implements Runnable can be like any other class as far as having methods and variables. That class could have a method that saves a reference to an object that you want to call to "feedback the server response to the object". The object that is passed to the method in the Runnable class would implement an interface that defined a method that the Runnable class could call to do the feedback.

Yes, have the class that is to be called by the Runnable class implement an interface that defines the method you coded. There would need to be a way to save a reference to that class in the Runnable class. It could be done in the Constructor or via a special method.
 
Robert Kennedy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for this clarification. I will give it a try.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!