ProgrammingVery hardProduct-company placement · Java

What happens when this class is compiled?

What happens when this class is compiled?
class Code implements Comparable<String>, Comparable<Integer> {
    public int compareTo(String s)  { return 0; }
    public int compareTo(Integer i) { return 0; }
}
  1. A. Compile-time error: Comparable cannot be implemented with two different type arguments
  2. B. It compiles; the overload is chosen by argument type
  3. C. It compiles but sorting throws ClassCastException
  4. D. Compile-time error only because both compareTo return 0

Correct answer: A. Compile-time error: Comparable cannot be implemented with two different type arguments

After erasure both are the same interface Comparable, so a class may not implement two different parameterisations of one generic interface (and the two bridge methods would clash).

More OOP with C++ and Java questions

Exams that ask this topic