LearnFrenzy
Sign in Take a free test

Polymorphism and Method Overriding · Find the Error · Question 8

Error: @Override on a parent's private method Answer, explanation & discussion

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

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

Main.java
class Base {
    private void log() { System.out.println("base log"); }
}

class Derived extends Base {
    @Override
    void log() { System.out.println("derived log"); }
}

public class Main {
    public static void main(String[] args) {
        new Derived().log();
    }
}
  • A log must be static
  • B Base.log is private and not inherited, so Derived.log overrides nothing and @Override is an error Correct
  • C Derived must call super.log()
  • D void methods cannot be overridden
Correct Answer: Option B

Without @Override it would compile as an unrelated method.

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