Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes override static methods. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "override static methods." Watch "override static methods." New topic
Author

override static methods.

Sriram Chintapalli
Ranch Hand

Joined: Dec 16, 2003
Posts: 59
I am a little confused as to why static methods cannot be overridden and why only static methods can override static methods if they can!any light on this will be appreciated.
David Hadiprijanto
Ranch Hand

Joined: Sep 14, 2003
Posts: 52
Hi Sriram,
If a method is declared as static in one of ancestor class, you can not declare a method with the same signature as instance method in the child class.
This is what it means by static method can not be overridden.
However, if a method is declared as static in one of ancestor class, you can declare method a method with the same signature as a static method in the child class.
This is called hiding, not overriding.
Similarly, if a method is declared as instance method in one of ancestor class, you can not declare a method with the same signature as a static in the child class.
So, in another word static method can not override or be overridden. It can only hide or be hidden.
When a method is hidden instead of overridden, the actual method that will be invoked is determined at compile time (statically bound), meaning, based on the type of the variable reference.
Below is a sample code. Notice that in line 3, the output is "Parent staticMethod", not "Child staticMethod", eventhough b is an instance of Child, because the reference b if of type Parent, not Child.
Hope this helps.

[ January 15, 2004: Message edited by: David Hadiprijanto ]
[ January 15, 2004: Message edited by: David Hadiprijanto ]
David Hadiprijanto
Ranch Hand

Joined: Sep 14, 2003
Posts: 52
Hi Sriram,
your original question was asking why static methods can not be overridden.
So, here is my 2 cents:
Since :
1. static methods are associated with the class, not the instance.
2. overriding is a means for polymorphism.
3. polymorphism is a way to define different behavior based on the what is defined for the instance.
4. static methods are statically bound (eventhough this may not be part of the why, but part of implementing static method can not be overridden).
Hence, it does not makes much sense to allow overriding for static methods, because it is associated with the class, not the instance.
Sriram Chintapalli
Ranch Hand

Joined: Dec 16, 2003
Posts: 59
thanks a lot for the detailed explanation David it really helped me understand the concept.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: override static methods.
 
Similar Threads
abstract and static
static methods cant be abstract. Why?
can we override the static methods ?
All about static methods
override