Access specifiers:
There are 3 modifiers in Objective C as @private, @protected (default), and @public. When classes are defined in Objective C, properties are defined as either public or private. Public properties are contained within the .h file and private properties are contained within the .m file. We also have the option of declaring an instance variable as private then that should not be accessible from anywhere. By default all the methods are public. Having said that, using some features of Objective-C, We can achieve these access specifiers.
All the methods we define in the interface file (.h) are public methods, They are accessible to the outside 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 the 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.
Here, it's a visibility modifier
@private
means that the ivar (instance variable) can only be accessed directly from within an instance of that same class. However, that may not mean much to you, so let me give you an example. We'll use the init
methods of the MyFirstClass.m class.MySecondClass inherit to MyFirstClass here we can access public number because it is public, anyone can access it and easily access the protected variable. It is declared by a superclass @ protected variables are available to subclasses. But we cannot use PrivateBool because it is private here Compiler Show Error.
SomeOtherClass does not access its show Compiler Error:
So @private protects ivars from access by an instance of any other class. Note that two instances of MyFirstClass could access all of each other's ivars directly; it is assumed that since the programmer has complete control over this class directly, he will use this ability wisely.
Property vs Instance Variable (iVar):
To declare a variable in an objective-c class, we have the following two ways:
- Property
- Instance Variable (iVar)
Here, Both have some difference.
For a private/protected variable, use iVar; for a public variable, use property. If you want to use the benifit of property attributes for a private variable, like retain, nonatomic etc., declare the property in the implementation file as a private property. For an iVar, you can use @private, @protected and @public. But these attributes only influence the access of its subclass, and has nothing to do with the access ability of its instances.
performance:
iVar is faster than property, as property will call the getter or setter functions. When you declare a property, the compiler will add getter and setter functions for the property.
@synthesize for property UPDATE:
@synthesize is only for property, not iVar. It will help the property generate an instance variable, the getter and setter accessors for the property, and use the generated instance variable in the getter and setter accessors.
0 Comments