Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces' relationship with the super reference

 
Kamil Hlubek
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

So a summary of that topic would be very helpful.

Yours sincerely,

Kamil
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Roel for sharing your knowledge

But in my view both code snippet would do nothing actually
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:But in my view both code snippet would do nothing actually

You are correct! I forgot to post the main method Luckily no rocket science required, here it is
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah,but main is the most important part of Java code
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic