vinsi vinsi - 1 year ago 91
Objective-C Question

Does objective c support private inheritance of protocol

Can a protocol can be inherited privately so that in next inheritance level this should not be accessed ?

Answer Source

Objective-C has no “private inheritance” (or “private conformance”) the way C++ does.

You can conform to a protocol without advertising your conformance in your header file. For example, you can conform to NSCoding “secretly” if you put this above your @implementation statement in your .m file:

@interface MyObject () <NSCoding>

That declares a class extension that adds the NSCoding protocol to the MyObject class.

However, anyone (including a subclass) can ask whether you adopt the protocol:

[[MyObject class] conformsToProtocol:@protocol(NSCoding)]
// returns YES

[[MySubObject class] conformsToProtocol:@protocol(NSCoding)]
// also returns YES, if MySubObject is a subclass of MyObject

and anyone can send an NSCoding message to a MyObject by casting the object first:

[(id<NSCoding>)someObject encodeWithCoder:someCoder]

And if you make a subclass of MyObject, and your subclass also declares that it conforms to NSCoding, then it almost certainly needs to call [super encodeWithCoder:] from its own encodeWithCoder: method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download