Which of the following statements are true? A) An inner class cannot be defined as private. B) Static methods can be overridden by static methods only. C) Static variables can be called using class name. D) Non static variables can be called using class name. The correct answer given is C But i think it should be B, C Am i right ?
Hi, The question is deliberately confusing. When you implement an identical static method in a subclass (as in your code example above), it isn't termed overridding as it would be with a non-static method. Both methods exist at the same time (whether or not there is an instance of a class) and only an object name qualifies which one you are referring to (otherwise in the subclass it will refer to its local method). In other words, its not the fact that you can't implement it, its the fact that it is not called overridding in such a case. Hope that helps. Richard
Joined: Apr 14, 2001
Okay i agree that it is not termed as overriding w.r.t to static methods but method hiding. But if i go with the compilation error which is :
m() in Example cannot override m() in Super; overridden method is static
Suppose the statement was as follows : Method in subclass cannot override static method of base class. Would it be correct ?
Joined: May 17, 2001
Indeed it would, but its probably simpler just to say 'Static methods cannot be overridden'. These were the sort of questions that really caught me out in the exam!
Hi Angela, 'Overriding' only relates to instances. A static method belongs to the class. See this Tech Tip at Sun for an explanation. Hope that helps. ------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform