wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about the explanation given for B&K exam question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about the explanation given for B&K exam question?" Watch "Question about the explanation given for B&K exam question?" New topic
Author

Question about the explanation given for B&K exam question?

Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
*spoiler* *question below is from the mock test exam from B&K*

The following question is similar to the one from the B&K CD. My question is more on the explanation they give for the answer.



EXPLANATAION given: Only instance methods can be overridden, and calls to super only apply to overridden methods.

My question...

For one, I don't see this rational as the reason for the compilation error at line #1. I don't see how the compilation error: "non-static variable super cannot be referenced from a static context" really applies to what they describe. I understand only instance methods can be overridden but not sure how that's really relevant to the error at hand?

Also I'm confused by the second part of the explanation where it states "and calls to super only apply to overridden methods." I don't see that as being true. If you remove the line marked #1 above and call a non overridden method from doStuff(), a call to super seems to work fine (including the static):



Am I missing something about the explanation or is it partially wrong?
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
if i understood corretly...

1 The reason for compilation error at line #1 : "non-static variable super cannot be referenced from a static context!" -> True

2 General statement "calls to super only apply to overridden methods." -> False

3 The explaination "Only instance methods can be overridden, and calls to super only apply to overridden methods."

-> True if the context is specific only to the question itself
-> False if not, as of 1,2



Hard work rewards
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Here's my understanding...

Suppose SubClass extends SuperClass. Then within SubClass, super is interpreted as (SuperClass)this. In other words, it is a reference to the current instance (this) upcast to the type of the immediate superclass. Consequently, super can be used only in a non-static context, which is why the error message applies.

The above works fine for fields, but due to polymorphism, ((SuperClass)this).method() would still invoke the overridden version. So in the case of overridden methods, "super" goes a little further and ensures that the body of the superclass method is invoked. Now, you can call super.method() for a method that's not overridden, but it will not have any special effect, because the superclass method is already inherited in the subclass.

(See JLS 15.11.12 and 15.12.4.9.)
[ December 13, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
Originally posted by marc weber:
Now, you can call super.method() for a method that's not overridden, but it will not have any special effect, because the superclass method is already inherited in the subclass.


Right, I understand that, and that totally makes sense (for non-overridden methods the call to super has no effect since you already inherited it and no need to use super), yet I think the explanation is a bit misleading to say "you can NOT call super from non inherited methods."

Thanks for the clarification.

As a side note, though, this brings up a good point, I was thinking that maybe the compiler should never let you call super.xx() on a case where you hadn't overridden xx(). A quick test, though, shows how you couldn't really do that since it's possible you could extend your class and override xx() in another subclass as the code below shows...



In real life this code would be totally stupid and a poor design - if the developer wanted to ensure that only C's fooBar method was called he should make fooBar final in C.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Rick Reumann:
...I think the explanation is a bit misleading to say "you can NOT call super from non inherited methods." ...

I understand what you're saying. But does the explanation really say that you "can NOT call super from non inherited methods"? Or does it just say that "calls to super only apply to overridden methods"? In the second case, I would interpret "apply to" as meaning "have an effect on." Does that make sense?
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
Originally posted by marc weber:

I understand what you're saying. But does the explanation really say that you "can NOT call super from non inherited methods"? Or does it just say that "calls to super only apply to overridden methods"? In the second case, I would interpret "apply to" as meaning "have an effect on." Does that make sense?


Sorry for the late reply (been busy , yes mark that makes sense, thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about the explanation given for B&K exam question?