LearnFrenzy
Sign in Take a free test

Polymorphism and Method Overriding · Find the Error · Question 5

Error: a static method replacing an instance method Answer, explanation & discussion

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

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

Main.java
class Engine {
    String start() { return "vroom"; }
}

class TestEngine extends Engine {
    static String start() { return "test start"; }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(TestEngine.start());
    }
}
  • A TestEngine must be abstract
  • B A static method cannot override (or hide) an inherited instance method with the same signature Correct
  • C Strings cannot be returned from static methods
  • D start must be final
Correct Answer: Option B

javac: "start() in TestEngine cannot override start() in Engine; overriding method is static".

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