To answer this question, we need to understand the concept of method overriding in Java.
In the given code, we have a class Test
with a static method show()
defined at line 3. The show()
method simply prints "Static method in Test" to the console.
Then, we have another class Q4
which extends the Test
class. In the Q4
class, we have a method show()
defined at line 10. This method is an instance method and it also prints a message to the console: "Overridden static method in Q4".
Now, let's go through each option to understand why it is correct or incorrect:
Option A) Compilation error at line 3 - This option is incorrect. There is no compilation error at line 3. The show()
method in the Test
class is a static method, and it can be called using the class name.
Option B) Compilation error at line 10 - This option is correct. The show()
method in the Q4
class is an instance method and it is trying to override the static method show()
in the Test
class. However, it is not possible to override a static method with an instance method. Therefore, a compilation error will occur at line 10.
Option C) No compilation error, but runtime exception at line 3 - This option is incorrect. There is no runtime exception at line 3. The show()
method in the Test
class is a valid static method.
Option D) No compilation error, but runtime exception at line 10 - This option is incorrect. There is no runtime exception at line 10. The compilation error at line 10 prevents the code from executing, so there won't be any runtime exception.
The correct answer is B. There is a compilation error at line 10 because it is not possible to override a static method with an instance method in Java.