LearnFrenzy
Sign in Take a free test

Generics · Find the Error · Question 5

Error: two overloads with the same erasure Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.List;

public class Main {
    static int total(List<Integer> nums) { return nums.size(); }
    static int total(List<String> words) { return words.size(); }

    public static void main(String[] args) {
        System.out.println(total(List.of(1, 2)));
    }
}
  • A total must return long
  • B Both methods erase to total(List), so they clash; give them different names Correct
  • C Static methods cannot be overloaded
  • D List.of needs a type argument
Correct Answer: Option B

javac: "name clash: total(List) and total(List) have the same erasure".

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links