This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
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


Win a copy of Head First Android this week in the Android forum!
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: 1294

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

You're welcome
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43368
    
  32
And welcome to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: override and redifinition?
 
It's not a secret anymore!