Can Objective-C protocols and categories be inherited? Can Objective-C protocols and categories be inherited? ios ios

Can Objective-C protocols and categories be inherited?


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.


Categories are like an extension to a class. You can add your own methods to other classes (such as NSString or anything else). Which means that any subclass gets the methods as well.

Whereas a protocol are a list of methods that requires the class that confirms to it to implement all of it (unless it uses the @optional tag). So there is no point of it's subclass to inherit it.

Edit:

For the protocol implementation, I realized I wasn't clear enough. Protocol Methods that were implemented in it's superclass can be inherited, however, what I meant was usually you don't need to override your superclass's protocol method.


Your question is unclear. You may be asking whether subclasses inherit the protocols and categories of their superclass. @theAmateurProgrammer has answered this.

You may also be asking whether categories and protocols can themselves inherit from other categories and protocols. For categories, the answer is no. For protocols, the answer is yes, and in fact protocols should almost always inherit just like classes like this:

@protocol SomeProtocol <NSObject>...@end

This says that anything that conforms to <SomeProtocol> also conforms to <NSObject> (which is a protocol as well as a class). This allows you to call methods like respondsToSelector:, which is very important for most protocol implementations.