• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

the use of "super" keyword

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.

 
Squanch that. And squanch this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!