• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why can't Static Methods be overridden?

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jean Fore
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For more details, see Overriding vs. Hiding.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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*.)
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Whatever you say buddy! And I believe this tiny ad too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic