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 methods & Overriding!

 
Nadeem Khan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We say that static methods arent overridden i.e they dont follow the same principles of overriding. For Example-



So my question is then why do we HAVE to declare our static method in derived class (with same name & arguments as parent's method) with same or stronger access modifier??
That is why cant i assign hey() of derived class a private access modifier when its not being actually overridden? I dont see any harm in it as it can't be called by derived's super classes!!
 
Daniel Chemko
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be using static methods like they're designed to be used for, by not calling them from instance objects.

Proper access to each of these methods would be:
Base.hey();
Derived.hey();

Here's an example to demonstrate static methods and static code blocks too.

 
Nadeem Khan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daniel,
Got your point about correct static usage. But my question was
that why cant i declare two static methods (with same name & args)
- in the derived class with a private access modifier
- and as public in base class
( as its not getting overridden) ?
 
Aarti Gauba
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We say that it is not getting overridden because the version of method being called is decided at compile time and not runtime. whereas in overriding, the version of method to be called is decided at runtime depending on the object type.

but for static methods, dis is decided at compile time only.so it is not overridden. but it does not mean it is not following the rules of overriding. it follows the rules of access modifiers and exceptions n all...when 2 methods with d same name ,same arguments ,etc..etc.., are there in 2 classes...it is called overiding...and it is d same for static methods... but dey are not being overriden.... soo the diff between calling it overriding..and actually being overriden...

uufff....
 
Nadeem Khan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats my point..since its not getting overridden why to follow those rules? Suppose static methods dont follow those rules, its not going to create any exception or confusion. The rule is simple ..If you are a class (or an instance) and you have a static method, you can only call your own method (with that specific name & args!) OR you can call the static methods you have inherited (but not declared your own methods with same name & args)!!!
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not because of any rules, it is a consequence of how computers work. There is only one, and can be only one of anything that is static: Whether function or not. It gets laid out when the compiler/jvm working together make a whatever it is the programmer has written.

If you need more than one of them, you call new. If the coder makes it static, then it goes when the compiler loads the class and is a consequence of the way machinery operates ~ not a consequence of any rules.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic