0

Possible Duplicate:
Understanding @Protocols in Objective-C
Objective C protocols usage

I just started learning objective C and I do not seem to get my head around protocols very well. My understanding of Protocols in objective-C is that, you specify some method definition without actually writing the code for it. What that means is that, whoever decides to inherit my class must implement all my required methods.

My question here is that, Isn't protocols creating an extra over-head which is not really needed. If I need a method in my new class I can just implement it. Why do I need to inherit from a protocol?

Why cant I just ignore using protocols and just create methods as I need them.

Community
  • 1
  • 1
tawheed
  • 5,565
  • 9
  • 36
  • 63
  • possible duplicate of [Understanding @Protocols in Objective-C](http://stackoverflow.com/questions/5738428/understanding-protocols-in-objective-c), [Purpose of categories and protocols](http://stackoverflow.com/questions/6394915/objective-c-purpose-of-categories-and-protocols?rq=1) – jscs Jul 18 '12 at 04:29
  • «whoever decides to inherit my class must implement all my required methods.» No. Subclasses will inherit their superclasses' implementations of protocol methods just as they would any other method. Protocols are just a grouped declaration of methods. – jscs Jul 18 '12 at 04:32

2 Answers2

1

Among other things, protocols are a way of letting the compiler help you avoid common errors. In this case, you can specify that one class will be calling specific methods on another class (often a delegate). The compiler will then check to make sure the other class (delegate) actually implements those methods and, if not, give you a warning message. Getting a message at compile time is preferable to crashing at runtime due to an undefined selector (method).

Turix
  • 4,470
  • 2
  • 19
  • 27
0

My question here is that, Isn't protocols creating an extra over-head which is not really needed.

there is no added overhead.

If I need a method in my new class I can just implement it. Why do I need to inherit from a protocol?

well, you can certainly declare a subclass for your common implementation, if that is an ideal path for your needs. if you try this, you will likely run into the issues i outline below.

protocols are often used because they are not actual physical types. it is an interface of methods and/or other protocols. usually, they are small and specialized. since objc does not offer multiple inheritance, protocols come in really handy for short extensions.

look at types which are complex subclasses and inherit one or more protocols; take NSString <NSCoding, NSCopying, NSMutableCopying, NSObject> as an example. knowing that objc uses single inheritance, consider how you would implement this class and 'inherit' from all those protocols - then consider the effect it would have on clients when passing those types after you have implemented this for all Foundation types. the class hierarchy and interfaces get very messy. the number of variations in many classes would explode to accommodate all those types as parameters. most people would stop before that point, and just drop type safety -- also a very bad idea. with a protocol, you can have "implements this interface" and type safety all in a simple language feature (multiple inheritance gets quite ugly quite quickly).

justin
  • 104,054
  • 14
  • 179
  • 226