Author | Topic: Stack Overflow |
vishy greenhorn |
posted April 07, 2000 06:41 AM
Hi, Can anyone explain whats wrong in this code? and whats the output? and why?
[This message has been edited by maha anna (edited April 07, 2000).]
|
rag unregistered |
posted April 07, 2000 08:37 AM
"for variables,private and static methods there is no dynamic binding.while executing the statement for access of variables private method and static methods, only type of reference is checked not the object that reference variable points to." this might help you.
|
maha anna bartender |
posted April 07, 2000 11:42 AM
Vishy, Your program will give stack overflow. Because from main , you call the static method doIt() of the base class DD. Inside this method you create a subclass EE with new and assign back to a var of type base class DD. Since the static methods CAN NOT be overridden the base class version of doIt() is called again anf in turn it creates a new Subclass converts back to base class and calls superclass version of static method doIt() ..... and it goes on. It DOESN'T come out of the doIt() at all. The concept to get from your prog is static methods are NOT OVERRIDDEN. Depending upon the type of the ref the method is linked. If the above code would have been change to
|
Shiva ranch hand |
posted April 07, 2000 12:07 PM
static methods belong to class not to any particular instance of class, and even when you try to overrride, it wont. it will call the static method in the current class ------------------
|
vishy greenhorn |
posted April 08, 2000 04:58 AM
Thanks to all for clearing my doubt and thanks particularly to mahaanna. By the way mahananna how much % u got in the certification exam.
|
maha anna bartender |
posted April 08, 2000 07:12 AM
how much? It's a secret. . You guessed right... Not taken yet. regds maha anna
|
JRoch greenhorn |
posted April 09, 2000 09:48 AM
Maha, Thanks for your profitable explanation of what's happening in the code Vishy submited. About "Since the static methods CAN NOT be overridden": Static method CAN be overriden, by static method. It's even necessary sometimes. Consider this really minimalistic case involving a well known around example of static method:
Wouldn't we OVERRIDE THIS STATIC METHOD, that we'd have misleading and uncomplete behavior, when entering 'java Derived' from the command line: 1/ Derived.class would unexpectedly print 'Hello from Base' This said, I once more learned a lot from your explanation, about ref/static methods behavior I didn't suspected before. Thanks for this. JRoch [This message has been edited by maha anna (edited April 09, 2000).]
|
maha anna bartender |
posted April 09, 2000 10:41 AM
JRoch, What I said was correct. Static methods CAN"T be overridden. Instead they are hidden . What is Overriding ? The correct method must be called according to the physical object created right?. you can create an object of derived class and cast to a base class. In case of the overridden , the correct method is called accoding to the physical object created, NOT to the type of the ref to which this physical object is assigned to. But in case of hiding, when you created and assigned var types are SAME , it gives the illusion of as though it is overridden. But it is actually not. Try to assign to a Base class type var. Now you will see, the base class static version WILL be called NOT the actual physical object in case of static methods. But for overridden instance methods the CORRECT overrring method in the ACTUAL CREATED object will be called whatever type you cast/convert this object to. The code which you posted has some typo in it. Ignoring that, what is actually happening is , when you type 'java Derived' from command line, the JVM just loads the Derived and other needed classes and DOES NOT INSTANTIATE Derived class. It just calls the static method main(..) of the class which comes after 'java' at the command line. Compile the foll. code and see for yourself.
[This message has been edited by maha anna (edited April 09, 2000).]
|
JRoch greenhorn |
posted April 09, 2000 12:25 PM
Maha, Here's what R&H says about "Using the same method name with identical arguments Afterwhile on page 170: This is clear enough for me. Sun JDK's compiler confirms this wording to be correct too: try in my example of overriding static method to make it more protected than it is in the Base class. Or as an alternative, add to it "throws Exception" that does not exist in the Base class method. The compiler now complains that it cannot "override" anymore, due to the rules of overriding not beeing respected. Also recent posts here or by Marcus site were concluding on the The fact that "hiding" better describes what really We most often see overloaded/overriden methods in Mocks I'll use it from now. JRoch PS: I've replaced "Good Bye JRoch" with "Good Sunday JRoch" [This message has been edited by JRoch (edited April 09, 2000).]
|
maha anna bartender |
posted April 09, 2000 01:34 PM
Well, I am glad you finally say sort of OK for me. . But I still stand on my point. If the qstn says 'override' instead of hiding, If I have to choose a answer, then I pretend myself that they meant this concept. regds maha anna
|
Jim Yingst sheriff |
posted April 09, 2000 03:21 PM
This is the sort of thing that annoys me about RHE - they use careless and/or incorrect language which is probably OK for the exam (since they helped write the darn thing) but which contradicts the and just creates more confusion later. Note that when RHE discuss late binding, they never ever mention that late binding does not apply to static methods, which I think is a fairly important property of static methods. And it's that difference which is the basis for differentiating between overriding and hiding. I think the real problem is that, while the JLS is very clear on the distinction, many of the people who use Java (especially the earliest users) are from C++ backgrounds, where overriding has a different definition. Naturally enough, this definition seems to be what RHE and various other people fall back on in many cases, whether they realize it or not. Apparently this includes the people who wrote those compiler error messages. So, the C++ definition of overriding is pretty prevalent, and i guess for the exam it's best to assume that "overriding" really means "overriding or hiding". But in general the JLS still "outranks" RHE, and I think it's important to konw the differences between overriding and hiding.
|
| | |