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.
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.
Rob Mech, SCJP 1.5<br /><a href="http://www.robsprogrammingjunk.com/" target="_blank" rel="nofollow">http://www.robsprogrammingjunk.com/</a>
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.
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
Joined: Apr 05, 2007
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?
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 ]
Joined: Apr 05, 2007
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.
Joined: Mar 29, 2007
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.