• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

the use of "super" keyword

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I wonder, in the example below, where does the method from a superclass returns its output (a collection):



* why is it not necessary to attribute the superclass output to a variable, like below:

childrenfeatures = super.getChildrenFeatures(object);

Thanks for any help.

Kaiser
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In either version of this method, the return value is always the value of a member variable. The superclass method just checks if it's null, initializes it if it's not, and then returns it. The child one calls the superclass one to initialize it if needed, then adds stuff to it, then returns it.

This design makes me vaguely uncomfortable, and I think you feel the same way, or you wouldn't have written. This is one of those examples where extending a concrete class -- as opposed to extending an abstract one or implementing an interface -- leads you to write questionable code.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, it is necessary to assign the output of superclass method to a variable if the superclass method returns something and you need to use that value. However if you just want to execute superclass method you can just call super.method9) without assigning the output. Both the scenarios should work fine, just depends on what you want to do.
 
Kaiser Lautern
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not feel comfortable either. Especially because it makes no harm at all to repeat the instance variable in the sub-class but adds a lot to the readability of the code.

Thank you guys for the enriching responses. You're aces.

Cesar

Originally posted by Ernest Friedman-Hill:
This design makes me vaguely uncomfortable, and I think you feel the same way, or you wouldn't have written. This is one of those examples where extending a concrete class -- as opposed to extending an abstract one or implementing an interface -- leads you to write questionable code.

 
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic