This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
I have a doubt regarding below code snippet
OUTPUT : 9
Now if B extends A,it means that B also has method display().Also, B has a variable 'a' which is same as variable 'a' in parent class.Then why does it happen that we get the output as 9, even though B has inherited display() method and also it's own 'a' variable.
I know that instance variables are not overridden ,but I am really sorry I am unable to relate this fact with this problem.
I request to please be patient with me and give a reply.
Sudhanshu Mishra wrote:Then why does it happen that we get the output as 9, even though B has inherited display() method
Because B doesn't have its own separate copy of that method. A's implementation of that method is invoked, and precisely because variables are not overridden the only "a" variable available to that method is A's "a".