posted 20 years ago
It is illegal to "override" an instance method with a static method with the same signature.
It is legal to have a static method in the parent class and a static method with the same signature in the subclass. However, this is not overriding. This is called method hiding and they behave very differently.
Let me explain with a couple examples. First let's look at the behavior of overriding. For this, we'll need two classes with an overridden method (which is, of course, an instance method, as only instance methods can be overridden).
As we have overridden this method, we use dynamic method binding in order to determine which method to invoke. As the run-time type of both s1 and s2 is Sub, the method doIt in Sub is invoked in either case.
Now, let's use a pair of static methods. In this case the method in the subclass is said to "hide" the method in the superclass:
As you can see, we no longer use dynamic method binding in order to determine which method to invoke. Rather, the compile time type (rather than the tun time type) is used in order to determine which method should be invoked. As the compile time type of s1 is Super and the compile time type of s2 is sub, we invoke the doIt methods of Super and Sub, respectively.
I hope that helps.