wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Why can't Static Methods be overridden? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why can Watch "Why can New topic
Author

Why can't Static Methods be overridden?

Jean Fore
Ranch Hand

Joined: Feb 23, 2006
Posts: 33
I see a statement in Khalid Mughal book saying that "since static methods cannot be overridden, declaring an abstract static method would make no sense. "

Why can't we override a static method? Is it because static method is always associated with a particular class?
Jay Ashar
Ranch Hand

Joined: Oct 13, 2002
Posts: 208
Yes, that's because it belongs to a particular class and not to its instance. If you try to override a static method you will not get any error compiler would just hide the static method of superclass.


SCJP 1.4<br />SCWCD 1.3
Jean Fore
Ranch Hand

Joined: Feb 23, 2006
Posts: 33
Okay No wonder I did not get any error when I tried to override a static method. So I found the statement in the book and my program has a contradiction.
Thank you very much
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

For more details, see Overriding vs. Hiding.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jay Ashar:
Yes, that's because it belongs to a particular class and not to its instance.


Actually, that's cannot really be the full reason. There *are* languages (such as Smalltalk) in which class level methods are polymorphic, too. (In fact that is because in that language, classes *are objects*.)


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Ilja Preuss:
Actually, that's cannot really be the full reason. There *are* languages (such as Smalltalk) in which class level methods are polymorphic, too. (In fact that is because in that language, classes *are objects*.)


Yes, but in Java static methods belong to the class rather than the instance. The subtype has a different class and hence static methods in the subtype, even with the same signature, are completely different methods belonging to a completely different class. Why Java's designers chose to do this is another story and I think has more to do with what you're referring to.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Ken Blair:
Yes, but in Java static methods belong to the class rather than the instance.


In Smalltalk, methods that belong to the class are polymorphic, too.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Ilja, I think for most of us, the fact that this conversation is taking place in "Java in General (beginner)" is sufficient reason to assume that the context of the conversation is that we're talking about the Java language. Yes, any given thing we might talk about may be implemented differently in other languages. I'm not sure that it actually helps beginners to talk about it here though. Jean was asking why can't we override a static method - implicitly, in Java. Jay was answering in the same context. Your posts read like corrections to Jay's post, which would be very relevant if this were SmalltalkRanch.com. But since this is JavaRanch.com, I think they're more confusing than relevant. Am I misunderstanding your intent here?


"I'm not back." - Bill Harding, Twister
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jim Yingst:
Jean was asking why can't we override a static method - implicitly, in Java. Jay was answering in the same context.[/QB]


Yes. And "because they belong to a class" is not the correct answer, as far as I can tell, even in a Java context. Is it?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Hmmm... I guess I'd say it's correct but incomplete, good enough in my opinion for JiG Beginner and the question that was asked. The poster seemed to think so, anyway. I suppose it's a matter of how deep and complete an answer is desired, and what level of complexity is considered appropriate for a given forum. For a question posted in JiG Beginner, I favor keeping the answer as simple as possible, in hopes of not scaring beginners with too many complexities at once. If this thread were in JiG Intermediate, I'd me much more inclinded to expand on the details. So I'll agree with your earlier characterization of the answer as incomplete, but I don't see a lot of benefit in pursuing the details here unless the original poster indicates interest. If you disagree, OK, expand on the topic as you wish.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jim Yingst:
I guess I'd say it's correct but incomplete


I'd say it's at least misleading, as it's too easy to understand it to say that class methods could not possibly be polymorphic.

And if you don't interprete it that way, is it much more than saying "because they are static"?

The poster seemed to think so, anyway.


Yes - and I feared that he misinterpreted it.

I suppose it's a matter of how deep and complete an answer is desired, and what level of complexity is considered appropriate for a given forum.


Mhh, I'm not sure that "because that's the way it is in Java - in other languages it's differently" really is that much more complex.

For a question posted in JiG Beginner, I favor keeping the answer as simple as possible, in hopes of not scaring beginners with too many complexities at once.


That's laudable, and I probably should have been more aware of being in the beginner's forum and formulating my post accordingly.

But I still think that the original answer was at least misleading, if not wrong - and needed to be clarified.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Ilja]: Mhh, I'm not sure that "because that's the way it is in Java - in other languages it's differently" really is that much more complex

It's not. My impression from your posts was that you were wanting to go somewhat further than that. If that's not the case, then oops, never mind.

[Ilja]: But I still think that the original answer was at least misleading, if not wrong - and needed to be clarified.

Fair enough. Judgement call, I guess.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jim Yingst:
My impression from your posts was that you were wanting to go somewhat further than that. If that's not the case, then oops, never mind.


Frankly, I'm not sure I knew were I wanted to go...
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
Marc read the link. I got a question here.

Suppose we have a method declaration with same signature as that of parent class static method without the static keyword, then why do we get a compile time error.

Error looks strange

overridden method is static

If it is NOT overriding then why the error states like that. Shouldn't it be hidden method is static.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Vishnu: well one method is static and the other isn't. If they were both static this would be hiding; if they were both nonstatic, it would be overriding. Since it's one of each, it's not clear what we should call it, but anyway it's illegal. Some of these compiler messages aren't as clear as they could be, and sometimes they use terminology which is just plain wrong from the JLS persepective. However in most such cases (at least regarding the overriding/hiding rules), the underlying behavior of the compiler and JVM matches what the specifications say is supposed to happen. It's just that the error messages provided by the compiler are not as clear or correct as they could be.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can't Static Methods be overridden?
 
Similar Threads
Overriding static methods
Overriding static methods
*why* can't static methods be overridden?
Redefining a static method in subclass
Methods