One protocol can incorporate other protocols using the same syntax that classes use to adopt a protocol:
@protocol ProtocolName < protocol list > |
All the protocols listed between angle brackets are considered part of the ProtocolName protocol. For example, if the Paging protocol incorporates the Formatting protocol,
@protocol Paging < Formatting > |
any object that conforms to the Paging protocol also conforms to Formatting. Type declarations
id <Paging> someObject; |
and conformsToProtocol: messages
if ( [anotherObject conformsToProtocol:@protocol(Paging)] ) |
... |
need to mention only the Paging protocol to test for conformance to Formatting as well.
When a class adopts a protocol, it must implement the required methods the protocol declares, as mentioned earlier. In addition, it must conform to any protocols the adopted protocol incorporates. If an incorporated protocol incorporates still other protocols, the class must also conform to them. A class can conform to an incorporated protocol by either:
Implementing the methods the protocol declares, or
Inheriting from a class that adopts the protocol and implements the methods.
Suppose, for example, that the Pager class adopts the Paging protocol. If Pager is a subclass of NSObject,
@interface Pager : NSObject < Paging > |
it must implement all the Paging methods, including those declared in the incorporated Formatting protocol. It adopts the Formatting protocol along with Paging.
On the other hand, if Pager is a subclass of Formatter (a class that independently adopts the Formatting protocol),
@interface Pager : Formatter < Paging > |
it must implement all the methods declared in the Paging protocol proper, but not those declared in Formatting. Pager inherits conformance to the Formatting protocol from Formatter.
Note that a class can conform to a protocol without formally adopting it simply by implementing the methods declared in the protocol.
Last updated: 2008-02-05