Interfaces' relationship with the super reference

Kamil Hlubek

Ranch Hand

Posts: 49

2

posted 1 year ago

Hi guys,

could you answer me an instruction of how and when to use the super reference with interfaces ( like for example for default-methods ), please. I would be also very thankful if you could explain me when you do not need the super reference if you use it with interfaces.
I am asking you that, because this is one subject of Java 8, but I can not find a good guide to it and I want to understand it completely and not try it only out with a chance that something of my explanation could be false.

Kamil Hlubek wrote:I am asking you that, because this is one subject of Java 8, but I can not find a good guide to it and I want to understand it completely and not try it only out with a chance that something of my explanation could be false.

it would be nice to see which code attempts you have already tried. That's always the best approach to learn. And if you are in doubt, you can of course always post the code snippet and ask your questions in these forums.

You know from classes that super can be used to invoke the overridden method of the parent class. I would expect similar behavior with the instance methods of interfaces, so only the default interface methods not the static interface methods. And the syntax is also a little bit different from what you are used to with classesOutput:
Printed in Super
Printed in Sub
Printed in SuperClass
Printed in SubClass

Now static interface methods can be invoked similarly to class/static methods (using the interface name). And you are also not required to follow the class/interface hierarchyOutput:
Printed in Super
Printed in SuperClass
Printed in Sub
Printed in SubClass

Hope it helps!
Kind regards,
Roel

PS. The above only applies to Java 8. In Java 7 you can only have abstract interface methods, so the above code snippets won't compile.

Sachin Tripathi wrote:Yeah,but main is the most important part of Java code

Most of the times it definitely is, but in this case I disagree. Because it's just invoking the printMethod method on a SubClass instance. With some trial and error, I'm pretty sure you would have figured this one out yourself.