How to force my method to accept objects from external software?
- by Roman
I have a method which needs to take a callback object as an argument and then (at the moment when it's needed) my method will call a specific method of the callback object.
I wrote a class called Manager which has a method called addListener. As the argument for this method I need to use a callback object which is defined by the external software. So, I define the addListener in the following way: public void addListener(Listener listener).
Of course Eclipse complains because it does not know what Listener is (because the Listener is defined by the external software). The only think that I know (should know) about the Listener is that it has a method called doSomething. So, to pleasure Eclipse I add an interface before my Manager class:
interface Listener {
void doSomething();
}
public class CTManager {
...
The problem seems to be solved but then I try to test my software. So, I create a class called test. In this class I create an instance of the Manager class and try to use addListener method of this instance.
I also create a class Listener, instantiate it and give the instance to the addListener. And it's the place where the problem appears. Eclipse writes that addListener is not applicable to the given argument. I think it's because it expect something from my Listenr interface but gets something from the Listener class.
How can I solve this problem?