LearnFrenzy
Sign in Take a free test

Polymorphism and Method Overriding · Find the Error · Question 7

Error: overriding a final payment method Answer, explanation & discussion

  • Find the error
  • Easy
  • 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 Payment {
    final double gst(double amount) { return amount * 0.18; }
}

class ExportPayment extends Payment {
    @Override
    double gst(double amount) { return 0; }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new ExportPayment().gst(100));
    }
}
  • A gst must be static
  • B gst is final in Payment, so ExportPayment cannot override it Correct
  • C ExportPayment must be final
  • D 0 must be 0.0
Correct Answer: Option B

Remove final in the parent if subclasses should change it, or add a separate overridable hook.

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