Overriding Static Methods

K

Ken Kafieh

[I'm reposting this because it didn't see to go up the first time. sorry if
this is a repeat]

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 ( ) {
}
static void method2 ( ) {
System.out.println("Failure."); }
}

class ClassB extends ClassA
{ public static void main (String[] args) {
}
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
 
K

Ken Kafieh

Something went wrong when I posted that last message her is the code I meant
to use for the first example:

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

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



Here is the code for the second example:


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();
}
}
 
T

Tony Morris

Ken Kafieh said:
[I'm reposting this because it didn't see to go up the first time. sorry if
this is a repeat]

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,

You can not override a static method.
http://www.xdweb.net/~dibblego/java/faq/answers.html#q18
@see JLS 2nd Ed. for more information.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
C

Chris Smith

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!

That's sort of correct. Really, the better way to use terminology is to
say that you can't override a static method. Static methods are subject
to something called "hiding", in which a method of the same name and
signature in a subclass doesn't replace a method from the superclass,
but instead coexists with it such that some method calls resolve
(lexically, at compile-time) to the subclass implementation instead of
the superclass.

This makes a lot of sense, when you think about it. The entire point of
a static method is that it is called without specifying any particular
instance of the class. As such, it makes no sense to resolve the method
using an instance of the class.

Since you haven't given a working example of the code that doesn't work
(the code you provided had an empty main method, which means it prints
nothing at all), I can't give more specific advice. Please post a
complete example and I'll help you out a little more.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Smith

Ken said:
no matter what I do I cant seem to get to paste the code into a message.

I placed the code on a URL. here it is...

http://24.156.35.62/code.htm

Okay,

It's hard to get a clear picture of what is or is not an acceptable
solution to you. You aren't going to get polymorphism from a static
method, since they can't be overridden and there's no object to use as
the basis for dispatch anyway. So while it would work if you called the
method unambiguously as "ClassB.method1()" instead of just "method1()",
that's probably not what you have in mind.

Your best bet is to use instance methods as you did in the second
example.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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,723
Latest member
TwilaTarde

Latest Threads

Top