• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

question from master exam

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

What is the result?
A 4243
B 4342
C Compilation fails due only to an error on line #1
D Compilation fails due only to an error on line #2
E Compilation fails due to errors on both lines #1 and #2
F An exception is thrown at runtime

The answer provided is C. But i don't see any error on line #1.
Can anyone clarify this?



[HENRY: Added code tags]
[ October 28, 2006: Message edited by: Henry Wong ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can't use super and this in static context
 
Alina Petra
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried to compile the code above? There are no compilation errors.
 
Alina Petra
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, i was wrong. The code does not compile because of the super within the static context. I focused on something else that's why the things got a bit mixed up

The answer is:
C is correct. Only instance methods can be overridden, and calls to super only apply to overridden methods.

In fact this answer got me confused. I haven't noticed that the call to super is done within a static context. That can not work, you are right.

But the explanation (

calls to super only apply to overridden methods

) is not correct, do you agree?
 
santro
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yaa i tried it with commenting the #1 line,it was successfully compiled
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

Alina Petra wrote

But the explanation (


calls to super only apply to overridden methods

is not correct, do you agree?
-



The language specification says, that static methods aren't overridden but hidden.
If you do call it also overriding, then the sentence would be not correct. Because you cannot call a static method with super.

Anyway, I think it's just easier to memorize if you say: "You can't use super or this in a static context."


Yours,
Bu.
 
Alina Petra
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Because you cannot call a static method with super.



This is false. You CAN call static methods using super. Actually that was all about within my question.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alina Petra:

This is false. You CAN call static methods using super. Actually that was all about within my question.




Yes, you can call a static method using super. But in this case, the "this" (and "super") is not available -- since you are in a static context.

BTW, even if it is in context, it is not a good idea. You should access static methods via the class name (like AlternateFuel.getRating2()). It makes it easier to remember which methods are static and which are not.

Henry
[ October 28, 2006: Message edited by: Henry Wong ]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote

(...) you cannot call a static method with super.





As Alina already said, this is wrong.
I didn't knew that. But I also never tried until today.


Maybe, the note on page 104 (Chapter 2: Object Orientation) of the K&B book should be altered.
Says:

Note: Using super to invoke an overridden method only applies to instance methods. (Remember, static methods can't be overridden.)




Thanks, Alina

Bu.
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic