Why does the Java KeyListener Interface work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why does the Java KeyListener Interface work?

I implement the KeyListener to my class and overrides his methods. Why does it work that I can now use my keyboard only by implementing an Interface. I don't get where the functionality comes from? If I look at the docs I can see that it extends from the Superinterface EventListener. But I can't see what the fields and methods etc. from the EventListener.

9th May 2019, 8:58 AM
Stefanoo - avatar
1 Answer
+ 1
That's called polymorphism. By implementing (override) functions of an Interface or base class, every call to the base class (or interface) method is resolved at runtime. In this case to the instance of your derived object. So without knowing the exact implementation at compile time you use a contract (method signature). Be aware that the contract does not include behavior.
9th May 2019, 9:22 AM
Daniel Adam
Daniel Adam - avatar