Generally, a non-static method can't be overriden with a static method & vice versa. A static method is not overriden, but, it is hidden by a method in the subclass.
The same rules hold for method hiding as for method overriding EXCEPT that polymorphism is not applicable. With reference to your example, the code compiles, because, the method in KK1 class will hide the method of class KK. All the restrictions that apply to overriding, also apply to method hiding. For example,
(i) if you make the method
doStuff() in class KK1 to be non-static, you will get an error;
(ii) if you make the method
doStuff() in class KK1 to be private, you will get an error;
(iii) if you include a throws clause, throwing checked exceptions, in the method
doStuff() in class KK1, you will get a compile-time error;
(iv) if you make the return-type incompatible in the method
doStuff() in class KK1, you will get a compiler error.
So, all restrictions applying in the case of method overriding, also apply to method hiding. The difference between the two is subtle in that
method hiding occurs in static methods, which belong to classes, not to individual objects. Thus a call to a static method via an object is equivalent to a call to the same method, through the class' name. This leads to the conclusion that polymorphism is not available in static methods. The actual method to be called is NOT determined at runtime, but, at compile-time (early binding). Taking your example, the above difference between the two can be demonstrated very easily:-
The output obtained is:-
Inside foo() of class KK1. Inside bar() of class KK. Inside bar() of class KK1. Inside bar() of class KK. Inside bar() of class KK1. Hope, I have succeeded in clarifying your concepts.
[ November 14, 2006: Message edited by: Abdul Rehman ]