LearnFrenzy
Sign in Take a free test

Polymorphism and Method Overriding · Find the Error · Question 10

Error: two methods that differ only in return type Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

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

Main.java
public class Main {
    static int parse(String s) { return Integer.parseInt(s); }
    static double parse(String s) { return Double.parseDouble(s); }

    public static void main(String[] args) {
        System.out.println(parse("42"));
    }
}
  • A parse must be private
  • B The two methods have the same name and parameter list, differing only in return type, which is not a valid overload Correct
  • C Double.parseDouble does not exist
  • D main cannot call static methods
Correct Answer: Option B

Give them different names, such as parseInt and parseDouble.

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