Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with tipsmart q24

 
jordan gong
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
jordan gong
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!
What about 1)?
Can it overrides at most once?

 
Daniel Wu
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jane,
How about static methods? I heard that static methods cannot be overriden by the subclass? Please clarify that. Thanks a lot.
Daniel
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic