LearnFrenzy
Sign in Take a free test

Object Class and Wrapper Classes · Find the Error · Question 9

Error: toString() overridden without public Answer, explanation & discussion

  • Find the error
  • Easy
  • 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
class Product {
    final String name = "Pen";
    @Override
    String toString() { return "Product " + name; }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new Product());
    }
}
  • A name must be private
  • B Object.toString() is public, and the override cannot reduce it to package-private Correct
  • C toString must be static
  • D Product needs equals
Correct Answer: Option B

Write public String toString().

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