Questions from the sample questions by CB - CompSci

<p>
[quote]
7. Consider the following declarations.
public interface Comparable
{
int compareTo(Object other);
}
public class SomeClass implements Comparable
{
// ... other methods not shown
}
Which of the following method signatures of compareTo will satisfy the
Comparable interface requirement?
I. public int compareTo(Object other)
II. public int compareTo(SomeClass other)
III. public boolean compareTo(Object other)
(a) I only
(b) II only
(c) III only
(d) I and II only
(e) I, II, and III

[/quote]
</p>

<p>Why is II wrong? (Answer is A)</p>

<p>As far as I know, when you implement methods defined in an interface, the return type must match (III is wrong) and the parameter type must match (II is wrong). Hope that helps!</p>

<p>The Comparable interface provides a comparison mechanism for any object. </p>

<p>My guess is that you can’t put in the formal parameters Someclass because Someclass is not related to all other classes in the Java API. Object is at the top of the hierarchy, so you can say that any object is-a Object, but you can’t say that any object is-a Someclass.</p>

<p>Also, the return type and formal parameters must match that of the interface.</p>