LearnFrenzy
Sign in Take a free test

Polymorphism and Method Overriding · Find the Error · Question 4

Error: an override made private Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

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

Main.java
class Sensor {
    double read() { return 25.0; }
}

class SecureSensor extends Sensor {
    @Override
    private double read() { return 26.5; }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new SecureSensor().read());
    }
}
  • A read must return float
  • B read is package-private in Sensor, and an override cannot narrow it to private Correct
  • C SecureSensor must be public
  • D private methods cannot return double
Correct Answer: Option B

javac: "attempting to assign weaker access privileges".

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