class A{ public void show(){ System.out.println("This is A"); } class B extends A{ protected void show(){ System.out.println("This is B"); } public class Main{ public static void main(String[] args) { A a = new B(); a.show(); } } What is the output

  1. This is A

  2. This is B

  3. Nothing will be printed as output

  4. None of the above


Correct Option: D

AI Explanation

To answer this question, let's go through the code step by step:

  1. There are two classes defined: class A and class B.
  2. Class A has a method called show() which prints "This is A".
  3. Class B extends class A and overrides the show() method to print "This is B".
  4. Inside the class Main, the main() method is defined.
  5. In the main() method, an object a of type A is created and assigned a new instance of class B using the statement A a = new B();.
  6. The show() method is called on the object a.
  7. Since the object a is of type A, but refers to an instance of class B, polymorphism comes into play.
  8. The method show() that gets executed is the overridden method in class B, which prints "This is B".

Therefore, the correct answer is:

D. None of the above

Find more quizzes: