Getting to know Objective C



The instance variables are private and are only accessible inside the class implementation.

Accessing the Data Members:

The properties of objects of a class can be accessed using the direct member access operator (.)


Properties are introduced in Objective-C to ensure that the instance variable of the class can be accessed outside the class.

The various parts are the property declaration are as follows.

  • Properties begin with @property, which is a keyword
  • It is followed with access specifiers, which are nonatomic or atomic, readwrite or readonly and strong, unsafe_unretained or weak. This varies based on the type of the variable. For any pointer type, we can use strong, unsafe_unretained or weak. Similarly for other types we can use readwrite or readonly.
  • This is followed by the datatype of the variable.
  • Finally, we have the property name terminated by a semicolon.
  • We can add synthesize statement in the implementation class. But in the latest XCode, the synthesis part is taken care by the XCode and you need not include synthesize statement.

Objective-C Inheritance

@interface derived-class: base-class

Objective-C Polymorphism

Objective-C polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.

When the above code is compiled and executed, it produces the following result:


Objective-C Data Encapsulation

Data encapsulation is a mechanism of bundling the data and the functions that use them, and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.

Objective-C Categories

Sometimes, you may find that you wish to extend an existing class by adding behavior that is useful only in certain situations. In order add such extension to existing classes, Objective-C provides categories and extensions.


Ví dụ:


Objective-C Protocols

Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation. Protocols are implemented in the classes conforming to the protocol.

Here is the syntax for class conforming to protocol