enter image description hereReferring to below program, as per my understanding it should print value of a as 20, as run time we get class B object but it is printing a value as 10. Can someone please help me understand this? Thanks! [Check code here]
Asked
Active
Viewed 62 times
-1
-
can you edit and add the code – Tejus Prasad Jan 31 '16 at 15:59
-
Please check the image attached – Gyanendra Pratap Singh Jan 31 '16 at 16:03
-
1possible duplicate of http://stackoverflow.com/questions/14933555/java-inheritance-overriding-instance-variable , short answer would be "This demonstrates that there is one object with two distinct fields called a ... and you can get hold of both of their values, if the access permits it" – awsome Jan 31 '16 at 16:03
2 Answers
0
You're shadowing. Remvoe the duplicate int a = 10;
definition, and mark a
as protected in Class A
For more details on shadowing in Java (or in general) check out this answer.
0
Variables are not polymorphic in Java. Same variable declared in subclass does not override the value in super class.

Joydip Datta
- 439
- 3
- 8