| Author |
static methods-overridden
|
mad dy
Greenhorn
Joined: Jun 27, 2006
Posts: 8
|
|
hi, Static methods can't be overridden but they can be redefined (from K&B) class Animal { static void doStuff() { System.out.print("a "); } } class Dog extends Animal { static void dostuff() { // it's a redefinition, not an override System.out.print("d "); } public static void main(String [] args) { Animal [] a = {new Animal(), new Dog(), new Animal()}; for(int x = 0; x < a.length; x++) a[x].doStuff(); // invoke the static method } } in this how to recognise tht dostuff() is redefined??
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
|
Because it is declared static, it is hidden and not overridden.
|
 |
Ankur Sharma
Ranch Hand
Joined: Dec 27, 2005
Posts: 1234
|
|
Maddy, Exactly I don't knwo what you really want to know??? But if you will run this class. You will understand the logic. Actually What happens in the static method. The real thing which matters is Class Reference, although you have overrided the method but you are calling method still with super class reference. that's why it is calling super class method. That's it.
|
 |
mad dy
Greenhorn
Joined: Jun 27, 2006
Posts: 8
|
|
Hi, when I compi led and run this program i got o/p "a a a" wht my question exactly is by seeing the code it looks like overriding the method dostuff in subclass Dog... but actually it is redefining it.. I'm not able to understand the difference b/w them
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
|
You will see the difference if you remove the keyword static from the definitions.
|
 |
swarupa patil
Ranch Hand
Joined: Feb 22, 2006
Posts: 62
|
|
Hi Mad dy For your example the answer is correct. The reason behind is STATIC methods cannot be overridden but we can achive this.When we compile the program with static methods the compiler resolves at compile time that is it calls the super class static method. so when you try to execute the method on Super class reference variable using subclass object . The only super class static method execute.
|
 |
vidya sagar
Ranch Hand
Joined: Mar 02, 2005
Posts: 580
|
|
|
More Stuffs
|
 |
mad dy
Greenhorn
Joined: Jun 27, 2006
Posts: 8
|
|
Thanks All, Now I got the clear Idea. As the static method of Super class is hidden, so it is not inherited to the Sub Class. So there is no issue of overridding the super class static method in Sub class, and the sub class will treat its static method as it's own method... Am I Right???
|
 |
Andy Morris
Ranch Hand
Joined: May 30, 2004
Posts: 78
|
|
It's not overiding since static methods only exist in the class they are defined in. The fact a subclass has a static method with the same name is pretty much irrelevant. When coding it is recommended to refer to static methods by their class and not via an instance variable (e.g MyClass.doStaticStuff() and not myIntance.doStaticStuff()), which would clear up any confusion about which static method will be called. In the exam if you just look at the type the reference is referring to and not what class initialised it, you shouldn't have any confusion.
|
 |
Revathi Velu
Greenhorn
Joined: Jun 19, 2006
Posts: 8
|
|
|
Isn't the dostuff in the Dog class completely different method because of the lowercase s ???
|
Thanks<br />Revathi
|
 |
Naseem Khan
Ranch Hand
Joined: Apr 25, 2005
Posts: 809
|
|
Revathi you are right... Its not redefinition, rather its a altogether new method. I think he has done typo mistake. If you use this line... a[x].dostuff(); // invoke the static method instead of a[x].doStuff(); // invoke the static method It won't compile. Compiler will complain dostuff method not defined in Animal class. Remember java is case sensitive. Regards Naseem
|
Asking Smart Questions FAQ - How To Put Your Code In Code Tags
|
 |
Nilesh Patel
Ranch Hand
Joined: Feb 02, 2006
Posts: 91
|
|
|
is possible to access static method and variable using class object of this method and classs OR just using Class Name...
|
Nilesh Patel
SCJP 1.5 - 87%
|
 |
Atul Sawant
Ranch Hand
Joined: Jul 06, 2006
Posts: 304
|
|
^^Hello all, This is my first post. Yes, you can call a static method or class variable using the instance of the class where these static method or class variable are defined. However, we cannot call a non-static method or class variable using a class name. Please share your inputs on these if you have one.
|
Mission SCWCD. Mission SCJP Complete: SCJP 1.4 - 91%
|
 |
 |
|
|
subject: static methods-overridden
|
|
|