Reputation: 363
Class A
implements interface I
that requires method doAction()
. If I call a method from class A
of class B
, and pass "this
"(class A
) into that method, how can I call a method that lives in class A
from the method in class B
? For example:
class A implements I {
public void start() {
B.myMethod(this);
}
@Override
public void doAction() {
// Do stuff...
}
}
Class B {
public void myMehtod(Class theClass) { //How would I accept 'this', and...
theClass.doAction(); //How would I call the method?
}
}
I am doing this for purposes of a custom library, without knowing the exact name of the class that extends I
.
Upvotes: 0
Views: 65
Reputation: 39451
This is a very basic question about how interfaces work. I'd recommend trying to find a tutorial about them.
Anyway, all you have to do is declare a parameter with the interface as its type. You can invoke interface methods on variables of the interface type (or any sub interface or class that implements that interface).
Class B {
public void myMethod(I theClass) {
theClass.doAction();
}
}
Upvotes: 4