This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
this question is from K&B master exam...
the answer of this code is a compilation error at #1.. in the explanation of this program says "only instance methods can be overridden and calls to super only apply to overridden methods" i have not understood this...
please expalin me what is menas... and what is System.out.print() statement doing its only for input at the command prompt i guess???
We really want to help you pass this exam, but you have to help yourself too! What part of the answer is confusing to you? Are you clear on instance methods and instance variables vs. static methods and static variables? Do you understand how to access instance vs. static members?
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Joined: Aug 06, 2007
I know the difference, instance variables are related to the class . i.e defines outside the method and can be accesed only when object of class is created. While static variables are class variables, they can be accessed using the classname, not by creating the object of class.
I am not sure to access the instance methods and static methods??
Correct me if i am wrong.
Joined: Oct 14, 2002
Would you be willing to write some code that demonstrates the creation and use of static and instance class members?
Joined: Jun 05, 2008
Dinesh, You wrote "While static variables are class variables, they can be accessed using the classname, not by creating the object of class."
Of course you can access a static variable or method creating an object of the class !!! All objects of the class share the static variables: if one object modifies the value of a static variable, other objects of the class will share the same value. A static variable or method can be accessed from non-static members and static members, but a static method CAN'T access non-static members.
You have 2 versions of getRating() method, one marked static and other not ok,
from static context, in this case from main method, you can not access instance methods, so instance method getRating without static is not an option, but you can access static getRating() from main, but the problem here that the program use "super" to access a static member which is not alowed, so the result is :
C- Compilation fails due only to an error on line #1
disclaimer: I'm a very green greenhorn and am utterly subject to correction (and I welcome it) on all of this . . . .
I ran across this same Master Exam question yesterday. It suprised me as well. I figured it would compile and run since I wrongly assumed that super would have the effect of standing in for the name of the immediate parent class.
I then got quite confused by the "correct answer explanation" from the Master Exam (which I now regard to be in error).
As S. Singh mentioned, the provided Explanation text states the following, "only instance methods can be overridden and calls to super only apply to overridden methods".
I find that the first clause of the explanation isn't especially relevant.
Then as Satya points out above, the second part "calls to super only apply to overridden methods" is at least confusing. In my tests, non-static method code can legally, successfully call super.myMethod() even when no overriding happens, specifically when:
the calling method does not override anything in the super instance
the method of the parent object called is not overridden in the calling object
(Of course using super absent any overriding is probably always unnecessarily redundant).
In fact, while my NetBeans IDE shows a warning, a non-static method can even compile calling super.myMethod() when myMethod() is a static method in the superclass. It seems equivalent to calling the static method using ClassName.staticMethod() notation. Of course this makes sense I guess since you can call a static method through a reference to an instance from the same class.
The most helpful explanation of why the call won't compile came from Antonio who said:
So, if you call this or super from a static context, this doesn't work because this and super are linked to an instance of the class
Now, after chasing this down some, I would describe super in this context as behaving somewhat like a reference for accessing the the current object as if ignoring any overidding members (whether they exist in fact or not) of the subclass of which the object is an instance.
I guess another way to say this is that super seems to act like a reference (to the current object) that is typed as the object's immediate parent class and functioning as if dynamic method selection is disabled from stepping down into the overrides of the current object's lowest/actual type.
(Yuck. I'm sorry that I can't seem to find a simple way to express the above idea.)
But, as antonio points out, from a static method there is no way to reference the current instance, since the code runs apart from an instance of its class. Thus super can't compute. The ClassName.staticMethod() notation has to be used.
To recap, the Master Exam gives us the correct answer, I just think it is in error when it explains why, in the study guide material it offers.