my dog learned polymorphism*
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: 1280

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: 1280

You're welcome
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
And welcome to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: override and redifinition?
 
Similar Threads
confuse for static method
Query in the topic static
Static method : Redefinition or an override ?
static methods-overridden
for static