RSS

Tag Archives: Difference between Category and protocol

Protocols in Objective C

In this post we shall have a detailed discussion on what is called as the protocols in objective c.

Protocols: Protocols are Objective C version of what is called as the interface in java or C#.  A protocol is basically a list of methods which is shared among classes the methods in the protocols do not have a body they are meant to be implemented by the developer implementing the protocol for his/her class. The objective-c language does not support multiple inheritance (a class can only derive from one superclass), but much of the same functionality can be provided by protocols because a class can conform to several different protocols.

Difference between categories and protocols: The main difference between categories and protocols in that in a protocol you get some additional methods that your class has to implement while in a category you just extend the functionality of a class with few additional method so basically a protocol says, “here are some methods I’d like you to implement.” while category says, “I’m extending the functionality of this class with these additional methods.” To be more precise I would say that a protocol specifies what method a class will implement and a category adds methods to an existing class.

How to declare a protocol and how to implement it: Given below is the syntax on how to declare a protocol and how to implement it with your class.

Protocol declaration:  Given below is the syntax to declare a protocol


In the above code the name of the protocol is ProjectRules and you can notice that there are no curly brackets; this is because variables go inside the curly brackets and protocols have no variables associated with them.Code Explanation: You use the @protocol directive to declare a protocol followed by the protocol name and in between the protocol name and @end directive you declare all the methods for the protocol that you are declaring and later you can give body to those methods inside the class in which you will be implementing this protocol.

Syntax to implement a protocol: Given below is the syntax on how you will implement a protocol in a class

Code Explanation: In the above code you can see that the ProjectRules protocol has been implemented for the ProjectManager class. You use pointy brackets to implement a protocol for a particular class in objective C and in case if you are implementing more than one protocol then in that case just use a comma to separate the name of the protocol just like you do in java or C#

In the above code you can see that the ProjectManager class implements two protocols one is the ProjectRules and ClientHandling.

In objective C, you have protocols and categories.  Pointed brackets are associated with protocols.  Curved brackets (aka parentheses) are associated with categories. So just remember, “pointy protocol, curved category.

I hope that you have understood the concept of protocols in objective c and if you have any queries related to the protocols then feel free to ask us at iphone.learning@quagnitia.com or leave your query as comments until then happy iCoding and have a great Day. You may find the PDF document for this post here.

 
1 Comment

Posted by on September 23, 2011 in Beginners

 

Tags: , , , ,