Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static modifier : Overriding & Hiding

 
Sadhi Mushrif
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain to me the effect of a method of having a static modifier and it's effect while overrriding.
1.If both the overriding and overridden methods have a static modifier.
2.Only if one the 2 methods have a static modifier.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods can not be overridden, they can only be hidden.
If the method in the parent is static then the method in the child must be static. If the method in the parent is not static then the child method can not be static.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A static method of a subclass can hide a static method with the same name and parameter list in the super class, but a static method can not override a superclass method.
If a reference of the superclass type invokes a static method on an object of a subclass type, then the method of the superclass type is actually used--the method of the subclass is not. That's because a static method is a class method and is not bound to a particular instance of an object. Therefore, the actual type of the instance does not make any difference.
In response to your second question, the result would be a compiler error.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic