File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static methods

 
Higgledy Smith
Ranch Hand
Posts: 192
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I remember things better if I understand the underlying reason for the behavior. That said, can anyone explain why static methods cannot be overridden?

Thanks.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Higgledy Smith wrote:I remember things better if I understand the underlying reason for the behavior. That said, can anyone explain why static methods cannot be overridden?

Thanks.

Static methods can be hidden (but not overridden.) Why can't they be overridden? Because overriding uses polymorphism (which calls the correct method based on the actual type of the instance referred to by the reference.) But with static methods you have no underlying instance, since they are linked to the class as a whole. That's why you can't override a static method (but if you declare a static method with the same signature in a subclass, that static method will hide the superclass' static method in subclass code.)
 
victor kamat
Ranch Hand
Posts: 247
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is worth noting that static methods cannot be abstract.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
victor kamat wrote:It is worth noting that static methods cannot be abstract.

You are correct. Abstract methods are meant to be overridden, which static methods can not. Surprisingly, however, static methods can be marked final. A static method which is marked final can not be redefined in a subclass.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic