LearnFrenzy
Sign in Take a free test

Static and Final Keywords · Find the Error · Question 3

Error: overriding a final method Answer, explanation & discussion

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

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

Main.java
class Exam {
    final int passMarks() { return 40; }
}

class EasyExam extends Exam {
    @Override
    int passMarks() { return 30; }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new EasyExam().passMarks());
    }
}
  • A EasyExam must be final
  • B passMarks is final in Exam, so EasyExam cannot override it Correct
  • C The return type must be long
  • D @Override is not allowed on int methods
Correct Answer: Option B

javac: "passMarks() in EasyExam cannot override passMarks() in Exam; overridden method is final".

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