thanx huiying li for the link.But i am still unable to understand the concept of hiding by class methods(static methods) could u simplify it for me?one more doubt.in one of the mock exams,it was asked if static methods can be overridden?the answer to it was no.but can't u override them by static methods themselves if not by instance methods.pls clear this doubt if possible.
The answer is "no", static method cannot be overridden. If you try to override a static method with a non-static method, you will get a compile error, if you try to override a static method with another static method, you won't get an error, but it is not overriding, it is hiding. When you override a method in a subclass, if you were to create an instance of the subclass and then upcast it to the parent like this: Parent p = new Sub(); With overridden methods, the method of the subclass would be called. With hidden method, the method of the parent class would be called. That is the main difference. You don't get the late-binding that you do when you override non-static methods, hence it is not considered overriding. Hope that helps, Bill
rajashree ghatak
Ranch Hand
Joined: Mar 10, 2001
Posts: 151
posted
0
thanx Bill for the explanation.it has cleared my doubt.
Hi * static methods are not overriden * static methods are hided instead of putting my own code, i leave it to sun & hope u'll get it! ----------------fromSun--------------------------------------- A hidden class (static) method can be invoked by using a reference whose type is the class that actually contains the declaration of the method. In this respect, hiding of static methods is different from overriding of instance methods. The example: class Super { static String greeting() { return "Goodnight"; } String name() { return "Richard"; } } class Sub extends Super { static String greeting() { return "Hello"; } String name() { return "Dick"; } } class Test { public static void main(String[] args) { Super s = new Sub(); System.out.println(s.greeting() + ", " + s.name()); } } produces the output: Goodnight, Dick because the invocation of greeting uses the type of s, namely Super, to figure out, at compile time, which class method to invoke, whereas the invocation of name uses the class of s, namely Sub, to figure out, at run time, which instance method to invoke.