This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I am not sure the following two statements are true? q24. (1) Each method in a parent class can be overridden at most once in any one subclass (3) Overriding methods must return exactly the same type as the method they override The answer indicates they are true, but I doubt about them. Please help. jordan
Hi Jordan, They are both true. (1) When you override a method you have to use the same signature ; if you declare two methods with the same signature, in the same class, you get a compile error. (3) One of the rules of overriding is that the overriding method must have the same return type. See JLS �184.108.40.206 Hope that helps. ------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform [This message has been edited by Jane Griscti (edited June 20, 2001).]
Jane, How about static methods? I heard that static methods cannot be overriden by the subclass? Please clarify that. Thanks a lot. Daniel
Joined: Aug 30, 2000
Hi Jordan, A subclass can only override an instance method in the parent by creating one overriding method. Here's an example
1. this is a legal override of <code>method()</code> in <code>SuperA</code>. It has the same signature (method name and parameters) and the same return type. 2. produces a compile error, the previous method has the same signature, you cannot have two methods with the same signature in the same class. 3. produces a compile error, it is not legal for the same reason as (2) and it is not legal as an 'overload' because it does not have different parameters. 4 & 5 both overload the <code>method()</code> in <code>SubA</code>. They have the same name but different parameter lists. An overload method may also have a different return type. Daniel, 'static' methods cannot be overridden as they are not instance methods; overriding only applies to instances. See the Sun Tech Tip on Shadowing. Hope that helps. ------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform [This message has been edited by Jane Griscti (edited June 23, 2001).]