Topic:   What is the output of the following Java program?
Apr 22, 2021 12:44 1 Replies 79 Views SANJAY

What is the output of the following Java program?

  1. class BaseTest
  2. {
  3. void print()
  4. {
  5. System.out.println("BaseTest:print() called");
  6. }
  7. }
  8. public class Test extends BaseTest
  9. {
  10. void print()
  11. {
  12. System.out.println("Test:print() called");
  13. }
  14. public static void main (String args[])
  15. {
  16. BaseTest b = new Test();
  17. b.print();
  18. }
  19. }
Prev Next
Topic Replies (1)
  1. 1
    idnkx user

    RANJIT

    Output
    Test:print() called
    Explanation
    It is an example of Dynamic method dispatch. The type of reference variable b is determined at runtime. At compile-time, it is checked whether that method is present in the Base class. In this case, it is overridden in the child class, therefore, at runtime the derived class method is called.

Leave a Reply
Guest User

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect