Wednesday 27 February 2013

25. What is displayed when the following is executed?

25. What is displayed when the following is executed?



Choices:
a. Compile time error
b. Run time error
c. prints : Parent's method2()
           Parent's method1()
d. prints : Parent's method2()
           Child's method1()
Answer:-
C is correct.
The code will compile without any error and also will not give any run time error.
The variable p refers to the Child class object.
The statement p.method2() on execution will first look for method2() in Child class.
Since there is no method2() in child class, the method2() of Parent class will be invoked and thus "Parent's method2()" will be printed.
Now from the method2() , there is a call to method1().
Please note that method1() of Parent class is private, because of which the same method (method1() of Parent class) will be invoked.
Had this method(method1() of Parent class) been public/protected/friendly (default), Child's class method1() would be called.
Thus C is correct answer.

No comments:

Post a Comment