Why the value changed? | Sololearn: Learn to code for FREE!

+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

5/15/2022 1:36:44 AM

Mai Anh

5 Answers

New Answer

+4

Because this.hole in class B is a protected member of class A. They are the same variable ...

+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.

+3

Might help in explaining the protected access modifier https://www.tutorialspoint.com/protected-access-modifier-in-Java

+1

Ipang I see, but why super.hole is equal to this.hole ?

+1

Thank you