Question 17) Which of the following statements are true?
1) static methods do not have access to the implicit variable called this 2) a static method may not be overriden 3) a static method may not be overriden to be non-static 4) a static method may not be overloaded
Answer given is 1 and 3. according to me 2 is also the answer.It is hidden and not overriden What say???
Static method can very well be overriden. So 2 is wrong Regards Anwar ------------------
Joined: May 08, 2000
No Anwar, They can never be over ridden. you can hide them by defining the same method in a subclass. but u can always access them using the class name. So it is proper to say static methods are hidden and improper to say they are overridden.
A parent class and a child class can have static funcions with same signature and return type. This means static methods can be overridden. So if you call a static method of a class and the method is not in that class, it tries to find a funcion with the same signature up in the class hierarchy. But there is a slight problem when you use objects to call static fucntions. Think of this situation. A static method amethod() in class A is overridden in sub class B. You create an object of B and assign it to an object variable of A. Now you call the static method using A.amethod(). It picks up the amethod() of the parent, not of the child. If amethod() were non static, it would have picked up the amethod() of B. The reason is static methods are resolved at compilation and compiler honours the type of object variable.