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?

class DD{
static void doIt() {
DD dd = new EE();
dd.doIt();
System.out.println("In DD");
}
}
class EE extends DD {
static void doIt() {
System.out.println("In EE");
}
public static void main(String[] args) {
DD.doIt();
}
}

[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


class DD{
static void doIt() {
EE dd = new EE();
dd.doIt();
System.out.println("In DD");
}
}
, then the subclass class EE version of doIt() will be called and there id NO static overflow.
regds
maha anna

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:


class Base{
void worthwhileReuseMethod{
//
}
public static void main(String a[]){
System.out.println("Hello from Base");
}
}

class Derived extends Base{}
public static void main(String a[]){ // overriding Base
// do SPECIFICS here;
System.out.println("Hello from Derived");
}
}


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'
2/ We'd need plan B to make the specifics beeing called anyway

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.
the output will be

Hello from Derived
Hello JRoch !I am from Base class //This is HIDING
Good-Bye! JRoch !I am from Derived class //This is OVERRIDING

regds
maha anna


class Base{
public static void sayHello(){
System.out.println("Hello JRoch !I am from Base class");
}
public void sayGoodBye(){
System.out.println("Good-Bye! JRoch !I am from Base class");
}
public static void main(String a[]){
System.out.println("Hello from Base");
}
}
class Derived extends Base{
public void sayGoodBye(){
System.out.println("Good-Bye! JRoch !I am from Derived class");
}
public static void sayHello(){
System.out.println("Hello JRoch !I am from Derived class");
}
public static void main(String a[]){
System.out.println("Hello from Derived");
Base d = new Derived();
d.sayHello();
d.sayGoodBye();
}

}

[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
Overloading and Overrinding, p.169 and p.170:

"Using the same method name with identical arguments
and return type is known as OVERRIDING".

Afterwhile on page 170:
"In a strict subclass of the class that defines the original method,
the method name can be reused with identical argument types and order
and with identical return type. This is OVERRIDING."

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
fact that "only static methods can override static methods".

The fact that "hiding" better describes what really
happens under the hood when we syntaxically override,
that is, mask a static method in a source code by
another definition for the same method signature
is undeniable, but less largely known.

We most often see overloaded/overriden methods in Mocks
exams and books. Hidden methods is not this common..

I'll use it from now.

JRoch

PS: I've replaced "Good Bye JRoch" with "Good Sunday JRoch"
in your example with oObject instanciations

[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.

|