+ 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.
1 Antwort
+ 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.