Forums Register Login

Problem with tipsmart q24

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 �8.4.6.3
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).]
+Pie Number of slices to send: Send
Thanks a lot!
What about 1)?
Can it overrides at most once?

+Pie Number of slices to send: Send
Jane,
How about static methods? I heard that static methods cannot be overriden by the subclass? Please clarify that. Thanks a lot.
Daniel
+Pie Number of slices to send: Send
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).]
I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1111 times.
Similar Threads
Null References
Mock Exam
null doubt
calling a method()
explain the answer
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:32:05.