| Author |
Method declaration question
|
Lakshmi Saradha
Ranch Hand
Joined: Oct 21, 2003
Posts: 170
|
|
The question is Which of the following statements are true? a. If an accessible superclass method is static, then any method with the same signature in a subclass must also be static. b. If a superclass method is synchronized, then the overriding method must also be synchronized. c. If a superclass method is public, then the overriding method must also be public. d. If a superclass method is native, then the overriding method must also be native. e. If a superclass method is protected, then the overriding method must be protected or public. The answers are a) c) and e) Option a) is "If an accessible superclass method is static, then any method with the same signature in a subclass must also be static" I am not able to understand the correctness of option a). Could anyone of you explain this? What does signature mean? Does it include the return type, method name , paramaters and exceptions.
|
Thanks,<br />Lakshmi.
|
 |
Detlev Beutner
Ranch Hand
Joined: Jul 13, 2001
Posts: 76
|
|
Hello Lakshmi,
"What does signature mean"
The JLS tells you...
8.4.2 Method Signature The signature of a method consists of the name of the method and the number and types of formal parameters to the method.
So why is [a] correct? Because an instance method cannot override a static method. And why not? Because the term "override" really does not work here. A static method is a method without an instance context. If you would try to override such a method (without being static), you try to bring in an instance context, which the "overridden" method does not know. So it makes really no sense, because one cannot call it "overriding". Hope it helps Detlev
|
 |
Cathy Song
Ranch Hand
Joined: Aug 24, 2003
Posts: 270
|
|
Hi Lakshmi, I had written the code below, which helped in figure out why static methods are hidden and not overridden. It is a little long, but it was quite useful to me. Thanks.
|
 |
Lakshmi Saradha
Ranch Hand
Joined: Oct 21, 2003
Posts: 170
|
|
Thank you Detlev adn cathy. Cathy thanks a lot for the code. I understood that Static methods are redefined and not overriden. I also have another question. is overriding always concerned with Object methods instead of class methods'?
|
 |
Arulkumar Gopalan
Ranch Hand
Joined: Oct 13, 2003
Posts: 104
|
|
is overriding always concerned with Object methods instead of class methods'?
The basic principle behind overriding is ' A super class variable can refer a sub class object ' . i.e. Dynanic method dispatch - In overriding depends upon the object being referred corresponding method will be invoked. So, it should be concerned with object methods only.
|
Anbudan & Mahalo,<br />Arul<br /> <br />-Not a sun certified Java professional :-)
|
 |
 |
|
|
subject: Method declaration question
|
|
|