Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question from master exam

 
Alina Petra
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
santro
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can't use super and this in static context
 
Alina Petra
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried to compile the code above? There are no compilation errors.
 
Alina Petra
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yaa i tried it with commenting the #1 line,it was successfully compiled
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic