• 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

Use of "super"

 
Ranch Hand
Posts: 50
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, again. Still working through the Javanotes text by Eck. I am in Chapter 9 now.

A few chapters ago, I came across this sentence, and made a flash card out of it.

The major use of super is to override a method with a new method that extends the behavior of the inherited method, instead of replacing that behavior entirely. (bold his)



Despite believing I understand "super", I have a terrible time with this flash card, and I don't think I'm understanding what he's trying to get across.

When I see "The major use of super is..." I always say "to gain access to methods in the superclass." (I know it can also be used to access constructors and hidden fields, but I'm going for the major use.)

Is that what he's saying, too, and I am just having a hard time parsing his grammar? Or do I have a fundamental misunderstanding of the major use of super? I have read what the Java Language Specification has to say about super, as well as the tutorials on the Oracle website, but didn't find those to be any help.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your understanding is correct. for example, say you are overriding a method in subclass . now when you call the method , it just print/process what *it has* .
some time you need to call the same version of super class method either begining or end of you override method(subclass method.) so how do you call?

implicitly it will be like this


so you have to explicitly call the method with immediate super class reference
i.e,


for instance, see how to override Object#finalize .

hope, you understand my point
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read "major use" as "main use" or "primary use" -- what he's saying there is that if you don't use super, then you're essentially replacing the functionality of the parent class method with the new code in the subclass method. When you use super, you are extending the original behavior with the subclass code that gets executed after the call to super.
 
Ken Austin
Ranch Hand
Posts: 50
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, guys.

Junilu, I wish he had just said it your way. That would be easier to understand.

Seetharaman: Yes, I did. Thank you for the reassurance and the explanation.
 
reply
    Bookmark Topic Watch Topic
  • New Topic