Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Overriding Static Methods

Reply
Thread Tools

Overriding Static Methods

 
 
Ken Kafieh
Guest
Posts: n/a
 
      05-23-2004
[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


 
Reply With Quote
 
 
 
 
Ken Kafieh
Guest
Posts: n/a
 
      05-23-2004
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();
}
}


 
Reply With Quote
 
 
 
 
Ken Kafieh
Guest
Posts: n/a
 
      05-23-2004
Hmmm
I just can't catch a break today
here is the URL to see the code that I was talking about:


http://24.156.35.62/code.htm


-Ken


 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      05-23-2004

"Ken Kafieh" <kkafieh-StopSp@> wrote in message
news:dR4sc.128035$ .cable.rogers.com...
> [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/...swers.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


 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      05-23-2004
Ken Kafieh wrote:
> 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
 
Reply With Quote
 
Ken Kafieh
Guest
Posts: n/a
 
      05-24-2004
Hi
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


thanks


"Chris Smith" <> wrote in message
news:...
> Ken Kafieh wrote:
> > 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






 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      05-24-2004
Ken Kafieh wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What do use instead of overriding static methods? Jim T Java 3 08-12-2009 06:59 AM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Overriding static methods : Whats wrong with these classes?? ankur Java 3 08-10-2008 08:28 PM
Overriding static methods - why doesn´t it work? Thomas Strandh Java 10 06-26-2005 11:26 AM
Why Petshop Changed all static methods to instance methods when upgrading from version 3.0 to version 3.1? Neo ASP .Net 1 01-07-2005 01:46 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57