Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static methods

 
Chris Cleverley
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a static method cannot be overriden to be non-static but is the reverse true i.e, can a non-static method be overriden to be static ?
After running a sample code I get a compiler error.
Chris
 
Aru
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,
Keep in mind that - When overiding a method, it can't be made more "PRIVATE" than the parent method.
Aruna
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris,
Static methods are attached to classes instead of instances. Hence they don't exhibit polymorphism and the term "override" does not fit into the semantics of static methods.
When you re-declare a parent non-static method as a static method in the subclass, you will be hiding the instance method in the parent class which is illegal. You will get the following compilation error
StaticOverrideDemo.java:8: The static method void doSomething() declared in clas
s Sub cannot hide the instance method of the same signature declared in class Ba
se. It is illegal to hide an instance method.

Hope this helps,
Ajith
 
Chris Cleverley
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it. Thanks you all.
Chris
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic