Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the difference between overriding a method and redefining a method ?

 
Vijay Tyagi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static methods cannot be over ridden
But why is it that they can be overridden when downcasting is used ?

 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite sure what you mean by downcasting? can you elaborate.

Hunter
 
Vaibhav Mittal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since static methods are class members and not instance members, the method that is corresponding to the type of the type of the reference variable will be called.

In your example,


is equivalent to converting the object of class Animal to object of class Dog and then calling the method on it. This works because the class that the compiler sees is Dog class and hence invoke the method corresponding to Dog class.

In the next case, since a[] is an array that holds Animal objects, the method corresponding to Animal class is invoked.

I hope it clears your doubt.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49453
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try writing an SSCCE which demonstrates your question, then see what happens when you run it. You will remember so much more of what you do than of what you are told.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic