+ 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?
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()'
+ 4
Which language?
+ 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;
}
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.