Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of "super"

 
Ken Austin
Ranch Hand
Posts: 39
Chrome Java
  • Mark post as helpful
  • send pies
  • 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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 39
Chrome Java
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic