To answer this question, let's go through the code step by step:
- There are two classes defined: class A and class B.
- Class A has a method called
show()
which prints "This is A".
- Class B extends class A and overrides the
show()
method to print "This is B".
- Inside the class Main, the
main()
method is defined.
- 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();
.
- The
show()
method is called on the object a
.
- Since the object
a
is of type A, but refers to an instance of class B, polymorphism comes into play.
- 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