Overiding Static Methods

K

Ken Kafieh

Hi,

When you override a non-static method, a call to that method from the super-class properly results in the sub-class version of that method being called. BUT, when you override a static method, it appears --to my frustration-- that a call to that method from the super-class results in the super-class version of the method being called!

Here is an example of what I mean. My intention is for the following to print out "Success" but it always prints out "Failure." instead.
It seems to ignore the fact that I have overrided method2( ) from ClassA with method2( ) from ClassB.

class ClassA
{ static void method1 () { method2(); }
static void method2 () { System.out.println("Failure."); }
}

class ClassB extends ClassA
{ public static void main (String[] args) { method1(); }
static void method2 () { System.out.println("Success."); }
}


I can get around the this problem by removing the static modifier, creating a third class, and instantiating a ClassB object. Like below. But I'd rather not have to create a third class, and instantiate an object if there is a way to to keep the code smaller,simpler,easier. Is there a way to fix the code above so that it prints "Success" instead? It just seems like there ought to be!


class ClassA
{ void method1 () { method2(); }
void method2 () { System.out.println("Failure."); }
}

class ClassB extends ClassA
{ void method2 () { System.out.println("Success."); }
}

class Start
{ public static void main (String[] args)
{ ClassB b = new ClassB();
b.Method1();
}
}

-Ken
 
M

Michael Borgwardt

Ken said:
When you override a non-static method, a call to that method /from the
super-class /properly results in the sub-class version of that method
being called. BUT, when you override a static method, it appears --to
my frustration-- that a call to that method /from the super-class
/results in the super-class version of the method being called!

It is so, and you can do nothing about it. static methods are bound at
compile time, i.e. the *compiler* decides which version of the method
is called. Getting it to behave as you want in your example is simply
not possible; it doesn't have the necessary information.
 
J

Josef Garvi

Michael said:
It is so, and you can do nothing about it. static methods are bound at
compile time, i.e. the *compiler* decides which version of the method
is called. Getting it to behave as you want in your example is simply
not possible; it doesn't have the necessary information.

Ken,

A work-around is to move the static methods into a singleton class. (the
methods would then become non-static, but the singleton would guarantee
that a class instance holding the methods always will be accesible). The
singleton can then be overridden, mimicking what you are looking for.

--
Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,727
Latest member
FelicaTole

Latest Threads

Top