Is making a iVar public in objective-C defeats the purpose of encapsulation?
@interface SampleClass{
@public
int totalRecords;
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In short, yes.
Direct access of ivars means that there is no way that subclasses or future versions of your
SampmleClasscan ever implement any kind of customized behavior whentotalRecordsis read or written.This is why access to object state is through methods everywhere but in the implementation of the class itself (and, even then, it is mostly through methods).