• 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

Overriding Static Methods, Huh?

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I'm having some trouble grasping a concept here for overrindg a static method. I just dont see why you would do this and even if you did, how it's supposed to function. I'm currently reading the SCJP5 Study (Sierra & Bates) guide and I just cant seem to understand the whole overriding thing.

What I get from the book is simply:

1. You can do it
2. If you do it doesnt do anything.

The book has the following code, which I follow completey however I just do understand *WHY* you would ever want to do this.

Can somone shed a little more light on the why and when you would want to do this. The book unfortuneately doesnt cover this in depth all that much.



Output is:

a a a

Is the whole thing here the dostuff() (Note no capital S) in the method name? I missed that the first time through but it would seem to be a typo since the it doesnt mention that the name is different. If that is not a typo why even mention it? It's just a different method name. If it is a typo then my first 2 questions apply. Why? When?

Thanks in advance for any replies. I just want to be sure i'm understanding the override concept completely.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,
Welcome to JavaRanch!

Let's assume it is a typo since (as you noticed) the example doesn't show much without that assumption. The example is showing that Animal.doStuff() still calls Animal's version of the static method. While you are allowed to create a subclass method with the same signature, it won't get called unless you explicitly write Dog.doStuff().

As for why one would want to this: If someone wanted to write confusing code, this approach could be useful . Seriously, I would avoid doing this in practice as it makes code that is hard to understand and can trip people up who aren't familiar with all the nuances of the language.

I think the book doesn't cover the why much because it isn't something you would want to do. But if the anti-pattern is on the exam, they have to cover it.

To summarize, you would want to override and instance method, not a static one.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually this is not an example of overridding.

Static methods cannot be overridden. If there is a static method in a subclass that has the same signature as one in the class it extends, then that is called hiding or redefining.

This differs a great deal from the concept of overridding.

When a method is overridden, then the invocation to be called is decided at runtime based on the runtime type of the caller.

When a method is hidden, then the invocation is decided based on the type of the reference.

Consider this example.



The output is

 
Rob Mech
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I used the word "Override" when I mean "Redefined" as the book says. But if I'm to interpret the last example correctly, does this redefinition take place during Instatiation only and differs from the statc method call itself?

Also, in the previous response you mention Dog.doStuff(); however in the code call, it does reference a Dog instance of the class.

I think i'm confused more now than before. Can we summarize this?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

static methods belong to the class.

They are NOT polymorphic like non-static methods.

If you have a ADS(*) as above,
only the reference type counts. Runtime type does not matter:

Animal poly = new Dog();
poly.doStuff(); // static method

will call the static method of Animal, not of Dog when redefined as in the example.
A really overridden non-static method would call the Dog-method.



You could call the dog method through an instance of type Dog, not animal, so
Dog doggy = new Dog();
doggy.doStuff();

would call the Dog method.

And remember: This way of calling a static method through an instance is allowed, but bad and confusing. My Eclipse environment sets up a warning if I do so, BTW.

Therefor always say:
Animal.doStuff();
Dog.doStuff();



Yours,
Bu.

(*) ADS: animal dog situation
Welcome to the Ranch, Rob!
[ April 14, 2007: Message edited by: Burkhard Hassel ]
 
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
See Overriding vs. Hiding.
 
Rob Mech
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

And remember: This way of calling a static method through an instance is allowed, but bad and confusing. My Eclipse environment sets up a warning if I do so, BTW.



Youre right on the confusing part. Im only asking this question so when it shows on the test I can answer it.

Ok, let me summarize then to make sure I understand.

If a Static method is declared and redefined as in the first example in this thread. Any reference calls to it Dog.doStuff() or Animal.doStuff() call the doStuff() in the Animal class.

If I instance the object though, it calls the appropriate one so long as I reference the static method with the appropriate reference?

If this is correct then is the example above (read the first one from the book) incorrect? Or is the trick here referencing the class NAME itself i.e. Dog.doStuff vs. a.doStuff() ?

Does everyone see where I'm confused here? It would appear as if the book example is incorrect?

I'm sorry if this seems like I'm not catching on, but on this particular nuaince I'm not!
 
marc weber
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

Originally posted by Rob Mech:
...let me summarize then to make sure I understand.

If a Static method is declared and redefined as in the first example in this thread. Any reference calls to it Dog.doStuff() or Animal.doStuff() call the doStuff() in the Animal class.

If I instance the object though, it calls the appropriate one so long as I reference the static method with the appropriate reference? ...


A good way to learn is to write test code to investigate and/or verify your understanding. For example...

(Note: Java is case sensitive. So doStuff is not the same as dostuff. Without correcting this in your code, you will get very misleading output.)

So what do you conclude from the output of this program?
[ April 14, 2007: Message edited by: marc weber ]
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Mech:

If a Static method is declared and redefined as in the first example in this thread. Any reference calls to it Dog.doStuff() or Animal.doStuff() call the doStuff() in the Animal class.



Nope: Dog.doStuff always calls the Dog-method and Animal.doStuff always calls the Animal-method. objectreference.doStuff will call the Animal-method if objectreference is a variable of type Animal (even if the actual object it references to is really a dog). So, in your example a[x].doStuff() will result in the same behavior as Animal.doStuff() would, just because a is an Array of Animals.
[ April 14, 2007: Message edited by: Sasha Ruehmkorf ]
 
Rob Mech
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UGH!!!

I can't BELIEVE that I missed this. My Whole point of confusion here was why the sample code output an a instead of a b.

What I failed to notice (and would have smoked me on any test) was the fact that the array was declared as follows.



It's using the superclass for the reference. How could I have missed that? Now this ALL makes sense with regards to WHY the super method is called for the code. I finally got it

Now, my next big question here. How is this any different then overriding? Why the term "redefined"? Unless I'm still missing something they semantically do the same thing.

Thanks everyone for the patience. I do appreciate it.
 
Sasha Ruehmkorf
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Mech:
Now, my next big question here. How is this any different then overriding? Why the term "redefined"?


Keith cleared it all:

Originally posted by Keith Lynn:
When a method is overridden, then the invocation to be called is decided at runtime based on the runtime type of the caller.
When a method is hidden, then the invocation is decided based on the type of the reference.



If your doStuff methods were not static (and without the typo) your code would produce the output a d a.
In this case you have an overridden method.
 
Rob Mech
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks All, I got it.
reply
    Bookmark Topic Watch Topic
  • New Topic