Dear friends,
The following is a cram
test question which I don't agree with author's answer. Please tell me what's your answer and why? Your help is highly Appreciated.
====
Consider following two classes:
What will be the output of compiling and running class B ?
Select 1 correct option.
a. It will print 10.
b. It will print 20.
c. It will not compile.
d. It will throw an exception at Run time.
=============
My answer is 'b'. I compiled and tested code in linux machine with
java 1.4.2 and result is 20.
But the author's answer is 'c', the reason is : Although, class B extends class A and 'i' is a protected member of A, B still cannot access i, (now this is imp) THROUGH A's reference because B is not involved in the implementation of A.
Had the process() method been defined as process(B b); b.i would have been accessible as B is involved in the implementation of B.
For more information read Section 6.6.7 of JLS:
http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#36191 [ September 06, 2005: Message edited by: Barry Gaunt ]