I am little confused about some concepts around Objective-C protocols and categories.
Can protocols and categories be inherited by subclasses in Objective-C?
Categories are collections of methods that are added to a class at run time. Because Objective-C uses dynamic binding, this means that the methods defined in a category are available to the class and all of its subclasses. Specifically selectors are bound to methods at the point where they are invoked, not during compilation or when the program first loads. Categories are added to classes when they (the categories) are loaded.
Protocols define collections of method signatures that the classes conforming to them promise to implement. Once a class has declared that it conforms to a protocol its as if the methods are declared in that class's interface and the rules of inheritance are exactly the same: subclasses inherit the declaration and implementation of the protocol methods but may also choose to override the superclass implementation.
Protocols themselves can be extended to produce new protocols. consisting of a superset of the methods in the original protocol. In fact, just as most classes inherit from the NSObject
class, most protocols extend the NSObject
protocol (protocol names and class names are in different name spaces). This is so that objects declared as id<WhateverProtocol>
can be sent basic messages like -retain
, -release
and so on without generating compiler warnings.