Hi guys, I'm not sure I understand one of the answers on this question on
Glenn, Mitchell.
OCAJP Oracle Certified Associate
Java SE 8 Programmer Practice Exams (Kindle Locations 26072-26074). Enthuware. Kindle Edition.
Which statement regarding the following code is correct?
Select 1 option
A. The code will print 10 and 40 if // 3 is commented.
B. The code will print 40 if // 2 and // 3 are commented.
C. The code will not compile because of // 1.
D. The code will compile if the line marked // 2 is commented out.
E. None of these.
SO the correct answer is A. The explanation says that
you can access ( (A) c). i because i is public in A. Remember that member variables are hidden and not overridden. So, B's i hides A's i and since B's i is private, you can't access A's i unless you cast the reference to A.
So, why in order to access A's i I have to cast C to A? I mean I appreciate that you can't access B's i because it's private but C has a version of i as well hasn't it, because it's inheriting from B which is inheriting from A...