Why the value changed? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Why the value changed?

I used the super keyword to access the variable on the super class then I used the this keyword to access the value in subclass but it return the same value. I thought it might be 10 + 5 = 15. https://code.sololearn.com/cULpJ3YF1P4v/?ref=app

15th May 2022, 1:36 AM
Foxie
Foxie - avatar
5 Answers
+ 4
Because this.hole in class B is a protected member of class A. They are the same variable ...
15th May 2022, 4:02 AM
Ipang
+ 3
Value of `this.hole` you changed in B class' constructor (line 9) refers to the protected member `hole` defined at line 2 class A. You were adding `hole` by itself.
15th May 2022, 3:19 AM
Ipang
+ 3
Might help in explaining the protected access modifier https://www.tutorialspoint.com/protected-access-modifier-in-Java
15th May 2022, 5:41 AM
Ipang
+ 1
Ipang I see, but why super.hole is equal to this.hole ?
15th May 2022, 3:58 AM
Foxie
Foxie - avatar
+ 1
Thank you
15th May 2022, 6:14 AM
Foxie
Foxie - avatar