I am currently preparing for the OCA exam. When doing the training test that was included in the McGrawHill OCA preparation book, I stumbled upon a strange question, that I could not answer correctly. I think there might be an error in one of the answers:
Why is answer G incorrect?
How can i call .super on an interface? What am I missing?
If someone can help, thank you in advance!
posted 2 weeks ago
I will quote the excellent answer I got in another forum:
In this case, the explicit interface .super call is necessary!
so, the answer in the exam software is correct after all, although the behavior is somehow hidden.
Line B will not compile because we are trying to decrease the visibility of the interface method while overriding the method. All the methods in an interface are by default 'public'. While overriding the method has to have the same visibility has to be either same or more. As public access specifier has the highest visibility, the overridden methods can be only public.
Line D is correct. In case of default methods in interfaces, one definition of method is already there. We can override the method in the implemented class as is done in the question. To call the method definition available in the interface, we need an instance of the interface which we can get with the help of InterfaceName.super keyword. We cannot say Hero.m1() as it is not a static method.