• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Interfaces' relationship with the super reference

 
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah,but main is the most important part of Java code
 
Roel De Nijs
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic