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]
-
can you edit and add the codeTejus Prasad– Tejus Prasad2016-01-31 15:59:35 +00:00Commented Jan 31, 2016 at 15:59
-
Please check the image attachedGyanendra Pratap Singh– Gyanendra Pratap Singh2016-01-31 16:03:17 +00:00Commented Jan 31, 2016 at 16:03
-
1possible duplicate of stackoverflow.com/questions/14933555/… , 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– awsome2016-01-31 16:03:51 +00:00Commented Jan 31, 2016 at 16:03
Add a comment
|
2 Answers
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.