What will happen if the superclass method does not exist in the subclass while trying to override it?

  • It will automatically create a new method in the subclass with the same name.
  • It will lead to a compile-time error.
  • It will result in a runtime error.
  • It will use the superclass method without any issue.
In Java, when you try to override a method from a superclass in a subclass, the method in the superclass must exist; otherwise, it will lead to a compile-time error. Java enforces method signature matching during compile-time, so if the method doesn't exist in the superclass, the compiler will not find a method to override in the subclass, resulting in an error.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *