Why do we get the output even if string name is private? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why do we get the output even if string name is private?

I think we've set the string 'Name' private. It's been encapsulated. Still, when that function is called again we get the required output. How does this happen?

12th Apr 2018, 7:13 AM
Ankush Dhondge
Ankush Dhondge - avatar
4 Answers
+ 10
When a property is private you can't access it directly like 'object.property' but you still can indirectly (through getter function) like 'object.propFetch()'
12th Apr 2018, 7:58 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 4
Which language?
12th Apr 2018, 9:40 AM
Manual
Manual - avatar
+ 2
we can't access the string directly through the object of the class like 'object.str' but can be accessed through public functions like 'object.getStr()' . (supposing the string name as 'str' ) getStr(){ return this.str; }
12th Apr 2018, 8:10 AM
Naveen Singh Negi
Naveen Singh Negi - avatar
0
Agree with Valen.H. ~ and Naveen Singh Negi. The private keyword, is to help with code safety and better design. The user of the classes only gets to use the public items, which means they can't accidentally damage the integrity of data inside the class.
12th Apr 2018, 10:37 AM
Emma