This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why static methods can not be overridden

 
teja dharma
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why static methods can not be overridden ?
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the belong to class..not Object
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
 
Guilherme Bazilio
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods cannot be overriden because they belong to the class, not to an instance of that class.
A static method don't need an instance to be accessed.

So, when you think you are overriding a static method, the only thing you are doing is redefining it.

(If I'm wrong just tell me please :P)
 
Ram Prasad
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bazilio is correct.
static methods cannot be overridden.

you can define the same static method in different classes and this mechanism is called redefining. The compiler accepts it and doesn't throw any error. But in run time you wont get the expected results of overriding since it is not actually a overriding.
i guess this should clarify your doubt tejahyd
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ram Prasad wrote:Bazilio is correct.
static methods cannot be overridden.

you can define the same static method in different classes and this mechanism is called redefining.


I think it is known as hiding
 
Guilherme Bazilio
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sudipo shekhar wrote:
I think it is known as hiding


I think hiding is also a valid name. K&B book calls redefine.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic