-1

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]

3
  • can you edit and add the code Commented Jan 31, 2016 at 15:59
  • Please check the image attached Commented Jan 31, 2016 at 16:03
  • 1
    possible 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" Commented Jan 31, 2016 at 16:03

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Variables are not polymorphic in Java. Same variable declared in subclass does not override the value in super class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.