-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]

2 Answers2

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.

Community
  • 1
  • 1
DominicEU
  • 3,585
  • 2
  • 21
  • 32
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