aspose file tools*
The moose likes Beginning Java and the fly likes override and redifinition? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "override and redifinition?" Watch "override and redifinition?" New topic
Author

override and redifinition?

ge weipeng
Greenhorn

Joined: May 14, 2009
Posts: 2
Hello, i just have a problem with the following code:
class Animal{
static void doStuff(){
System.out.print("a");
}
}
class Dog extends Animal{
static void doStuff(){
System.out.print("b");
}
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();
}
}
They say that the method doStuff() in class Dog is not an override,but a redifinition.Why?What's the difference between them?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi!

Polymorphism (i.e. overriding methods) only works for non-static methods. Therefore the doStuff() in class dog is just shadowing the one from the base class because of the name clash but besides the two methods aren't related like overridden methods.

Have a look at this for an explanation of polymorphism ;-)

Marco
ge weipeng
Greenhorn

Joined: May 14, 2009
Posts: 2
Thank you so much!
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

You're welcome
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
And welcome to JavaRanch
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: override and redifinition?