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

static methods

 
vamsi kantamneni
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


in the above example given in K&B
It is clearly said that the static methods cannot be overridden. I have a few doubts regarding this
1. A method is said to be overridden if the signature of the method is same in both the base class as well as the inherited class. In the above given example, I know that the methods are static and they load in the stack. how that is different from overriding??
2. if i remove the static keyword in the inherited class (Dog in this case) what would be the result?? (I have tried in the ecliplse.. but couldnt make anything out of it!!)
3. What if i dont want to use the static keyword in the inherited class (dog->doStuff()) (I have tried this too.. it said you cannot override the static method.. I m not overriding or anything.. jus trying to write another non-static method with the same name!!)

Please help me out of this confussion.....
 
Guy Buyle
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vamsi kantamneni wrote:

in the above example given in K&B
It is clearly said that the static methods cannot be overridden. I have a few doubts regarding this
1. A method is said to be overridden if the signature of the method is same in both the base class as well as the inherited class. In the above given example, I know that the methods are static and they load in the stack. how that is different from overriding??
2. if i remove the static keyword in the inherited class (Dog in this case) what would be the result?? (I have tried in the ecliplse.. but couldnt make anything out of it!!)
3. What if i dont want to use the static keyword in the inherited class (dog->doStuff()) (I have tried this too.. it said you cannot override the static method.. I m not overriding or anything.. jus trying to write another non-static method with the same name!!)

Please help me out of this confussion.....


You can't override an static method but problem above is a kind of method hiding
every thing is clarified when you follows these link: http://www.coderanch.com/how-to/java/OverridingVsHiding

Regards,
Guy
 
Himai Minh
Ranch Hand
Posts: 1249
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods belong to their classes.
Polymorphism does not apply to static methods.
Animal.doStuff() will behave in "Animal" way. Dog.doStuff() will behave in "Dog" way.
How about this?


Why? myPet is an Animal and it behave in "Animal" way. Polymorphism won't apply to static methods in this case.
 
Ameen Imtiaz
Greenhorn
Posts: 11
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, overriding has to do with Polymorphism. The overriding methods are selected AT runtime based on the instance's type in the ref. variable at that moment.

Whereas, the static methods invocation is always decided at compile time. So if an instance variable invokes the static method then the compiler actual replaces the obj with the class (type) of that ref. variable. All this is explained on page 150 of K&B (Ch 2). Which states:

"
int frogs = f.frogCount; // Access static variable FrogCount using f

...This is merely a syntax trick to let you use an object reference variable (but not the object it refers to) to get to a staticmethod or variable...
... In other words, the compiler cares only that reference variable f is declared as type Frog...
"

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic