Public class Player implements Comparable{private String name;private int goalsScored;// other methods go herepublic int compareTo(Object otherObject){ __________________________________return (goalsScored – otherPlayer.goalsScored);}}What statement can be used to complete the compareTo()method?A. Player otherPlayer = otherObject;B. Object otherPlayer = otherObject;C. Player otherPlayer = (Player) otherObject;D. Object otherPlayer = (Player) otherObject;

Answer :

Answer:

The answer is "Option C"

Explanation:

In the given code, a child class "player" is defined, which inherits its base class that is "Comparable". In the child class, a private variable is declared, that is "name and goalsScored", in which name is string variable and "goalsScored" is an integer variable.  

In this class a method, compareTo is declared, that accepts an object as a parameter, inside the method a player class object is created, that value is held by the reference variable, and this function will return a value, and another option is wrong, that can be described as follows:

  • In option A, It is wrong, because there is not a reference variable.
  • Option B and Option D are wrong, because in object creation first, we use the class name, which is missing.  

Other Questions