Access from class B to A with Listener | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Access from class B to A with Listener

https://code.sololearn.com/c6AicfkMc7v2/?ref=app If a class like B in the example Code has implemented a Listener(e.g. MouseListener) and in class A is an Instance from B, how can I call a Method from A with the implementation of the Method in class B. The example Code makes it clearer what I mean. I have read about a callback function to do this. Are there more ways to do this or is that a general design failure from me?

9th Oct 2019, 8:39 PM
Stefanoo
Stefanoo - avatar
3 Answers
0
Why dont you just make void hello static and call A.hello() in the aListener method
10th Oct 2019, 1:40 PM
Lord HN
Lord HN - avatar
+ 1
Do you want to change the implementation in B based on the listener?
10th Oct 2019, 7:21 AM
Lord HN
Lord HN - avatar
+ 1
Lord HN cool idea but nope 😆 Let us say that the Listener is a MouseListener and the overriden method is mouseClicked or something similar. Now the Listener triggers and the method mouseClicked is called. And this method calls the method from A. I hope it's now more clear. 😅
10th Oct 2019, 9:09 AM
Stefanoo
Stefanoo - avatar