Java OCA OCP Practice Question 2346

Question

Which statements are true about the following code?

class SupX {/*from  w  w w  . j  a  va 2s . c  om*/
  public void set(Collection<?> c) {/*...*/} // (1)
}
class SubX extends SupX {
  public void set(List<?> l) {/*...*/}       // (2)
  public void set(Collection c) {/*...*/}    // (3)
}
//------------------------------------------
class SupY {
  public void set(Collection c) {/*...*/}    // (4)
}

class SubY extends SupY {
  public void set(Collection<?> c) {/*...*/} // (5)
}

Select the three correct answers.

  • (a) The method at (2) overloads the method at (1).
  • (b) The method at (2) overrides the method at (1).
  • (c) The method at (2) results in a compile-time error.
  • (d) The method at (3) overloads the method at (1).
  • (e) The method at (3) overrides the method at (1).
  • (f) The method at (3) results in a compile-time error.
  • (g) The method at (5) overloads the method at (4).
  • (h) The method at (5) overrides the method at (4).
  • (i) The method at (5) results in a compile-time error.


(a), (e), and (i)

Note

The erasure of the signature of (2) is different from the erasure of the signature of (1), i.e., overloaded, since signatures are not override-equivalent.

Therefore, of the three alternatives (a), (b), and (c), only (a) is correct.

The signature of (3) is the same as the erasure of the signature of (1), i.e., overridden.

Therefore, of the three alternatives (d), (e), and (f), only (e) is correct.

The erasure of the signature of (5) is the same as the signature of (4), and not the other way around, i.e., name clash.

Therefore, of the three alternatives (h), (i), and (j), only (i) is correct.




PreviousNext

Related