Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C - Private vs Protected vs Public

Tags:

objective-c

I am hoping for some clarification on how Private vs Protected vs Public works with respect to class members when programming in Objective-C - I thought I knew the difference (I've added some comments to my parent class Person with respect to the same), but the fact that the compiler did not complain when I tried to access a private ivar/member of a parent class via the subclass now has me confused.

Here is my Parent Class:

/*  Person.h */  #import <Foundation/Foundation.h>  @interface Person : NSObject  {     //We can also define class members/iVars that are of type private     //This means they can only be accessed by the member functions     //of the class defining them and not subclasses     @private     int yob;          //We can also define class members/iVars that are of type public     //Public members can be accessed directly     @public     bool alive;      //By default class members/iVars are of type protected     //This means they can only be accessed by a class's own     //member functions and subclasses of the class and typically     //also by friend functions of the class and the subclass     //We can explicitly define members to be protected using the     //@protected keyword      @protected     int age;     float height;     } @property int age; @property float height; @property int yob; @property bool alive;  @end 

Here is my derived class Man:

    /*      Man - Subclass of Person     */      #import <Foundation/Foundation.h>     #import "Person.h"      @interface Man : Person     {         //iVar for Man         float mWeight;     }     @property float mWeight;      @end 

And finally, here is main:

#import <Foundation/Foundation.h> #import "Person.h" #import "Man.h"      int main (int argc, const char * argv[])          {             NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];              //Create a Person object             Person * aPerson = [[Person alloc]init];              //Create a Man object             Man * aMan = [[Man alloc]init];              //Let's attempt to modify our Person class members             aPerson.height = 5.11; //Protected             aPerson.age = 21; //Protected             aPerson.yob = 2010; //Private             aPerson.alive = YES; //Public              //Let's now attempt to modify the same members via our             //derived class Man - in theory, the private members should             //not be accessible by the derived class man             aMan.height = 6; //Protected             aMan.age = 26; //Protected             aMan.yob = 2011; //Private             aMan.alive = YES; //Public             aMan.mWeight = 190; //Protected member of Man Class              [pool drain];             return 0;         } 

Shouldn't the compiler complain why I try to access aMan.yob above? Or by using @property & @synthesize (i.e. the setter and getter methods) have I essentially made that member protected and therefore accessible to the subclass?

like image 948
noobzilla Avatar asked Feb 02 '11 01:02

noobzilla


People also ask

What is the difference between private protected and public?

Broadly speaking, public means everyone is allowed to access, private means that only members of the same class are allowed to access, and protected means that members of subclasses are also allowed.

What is public/private and protected in OOP?

There are three access modifiers: public - the property or method can be accessed from everywhere. This is default. protected - the property or method can be accessed within the class and by classes derived from that class. private - the property or method can ONLY be accessed within the class.

What is public and private in class?

public - members are accessible from outside the class. private - members cannot be accessed (or viewed) from outside the class. protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes.

What is private in OOP?

Variables and methods defined with the private keyword may be accessed only by other methods within the class and cannot be accessed by derived classes. The private keyword is used in most object-oriented programming (OOP) languages, including C++, C# and Java.


1 Answers

The usual trick is to create a class extension inside the .m file and put your private/protected property there instead of in the header.

//Person.m  @interface Person()  @property float height  @end 

this hides the 'height' property

Another trick is if you want to create a readonly property is to declare it in the header as

@property(readonly) int myproperty 

but in the class extension as readwrite which allows your .m to modify the value using the getter/setter

@property(readwrite) int myproperty 
like image 148
AndersK Avatar answered Sep 17 '22 15:09

AndersK