Monday, May 30, 2016

Objective-C Access specifiers

Like in most of the languages, Objective-C doesn't have these Private, Public and Protected access specifiers. By default all the methods are public. Having said that, using some features of Objective-C, We can achieve these access specifiers.


Public Methods :

All the methods we define in the interface file (.h) are all public methods, They are accessible to out side world.

Private Methods :

We can declare methods in the implementation file (.m) without defining them in the interface file (.h) to make them private methods. In this case, they are not accessible to outside world. Not accessible even to the subclasses.

Protected Methods :

In OOPs, Protected means, Only subclasses can access. In Objective-C, We can take a category and write all the methods we need as protected and can only import that category file only in the sub classes we needed instead of importing in the other classes. Thus, those methods become protected as we are using that category file only in the subclasses.

That's how we can use the Objective-C features like Categories and Extensions for getting those access specifiers like Private, Public and Protected. Same thing works for variables also.

Hope this post is useful. Feel free to comment in case of any queries.