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 ]
Joined: Sep 14, 2003
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.
Joined: Dec 16, 2003
thanks a lot for the detailed explanation David it really helped me understand the concept.