I overload my static methods all the time, so they can be overridden...
SCJP 1.4, SCWCD 1.4
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler et al, Refactoring: Improving the Design of Existing Code, 1999
Static methods are *not* overridden. Semantic nitpickiness follows:
�188.8.131.52, JSL If a class declares a static method m, then the declaration m is said to hide any method m', where the signature of m is a subsignature (�8.4.2) of the signature of m', in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method.
"Swapna", please read our JavaRanch Naming Policy, and change your displayed name to conform. We require two names in the format <first name><space> <family name>, preferably your real name. Thanks -Barry
static methods are linked a compile time (like variables) so if you override them the get hidden.
if u take this code u can call the static method with out create a instance static members are direclty related to the class
the output was staticMethod() in Subclass Test. 15 staticMethod() in Class Base. 5
So when u call the static method using the Child Class i.e DemoTest.staticMethod() it will hide the base class implementation and it executes the child class implementation.
again if u call the method using the DemoBase.staticMethod() it will execute the base class implementation ...
so instance does not hava a role in this case ... So static methods can be override but it was linked to the compile time(static) so it will try to hide the implementation....
supoose take this scenario
the output was ..
staticMethod() in Class Base. 5 staticMethod() in Subclass Test. 15 staticMethod() in Class Base. 5
see when i was calling DemoTest1.staticMethod() it give me the output of base class since DemoTest1 child class of DemoBase was not overridding the staticMethod() so the implemention of the base class was not hidden..
Create a child class object and assign it to Parent reference.
Now call any static method that has same signature in both the classes(parent & child), ur parent static method will be called since ur reference is of parent type.....
Even though ur object is of child type.... the binding happened during compile time and during compile time the compiler knows only about the type of reference(parent) not the object(child) to which it is refering to.That is why ur parent static method will be called.
I can't take it! You are too smart for me! Here is the tiny ad: