LearnFrenzy
Sign in Take a free test

Polymorphism and Method Overriding · Find the Error · Question 9

Error: an ambiguous overloaded call with null Answer, explanation & discussion

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

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

Main.java
public class Main {
    static String show(String s) { return "String"; }
    static String show(Integer i) { return "Integer"; }

    public static void main(String[] args) {
        System.out.println(show(null));
    }
}
  • A null cannot be passed to methods
  • B null fits both String and Integer and neither is more specific, so the call is ambiguous; cast it, e.g. show((String) null) Correct
  • C show must not be static
  • D Overloads need different names
Correct Answer: Option B

With show(Object) and show(String), show(null) would choose String, the more specific type.

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