LearnFrenzy
Sign in Take a free test

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

Error: overriding getClass() Answer, explanation & discussion

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

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

Main.java
class Disguised {
    @Override
    public Class<?> getClass() { return String.class; }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new Disguised().getClass());
    }
}
  • A Class<?> is not a type
  • B getClass() is final in Object, so it cannot be overridden Correct
  • C String.class does not exist
  • D Disguised needs a constructor
Correct Answer: Option B

This guarantees getClass always reports the real class.

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