0

I got a sample code from Protected methods in Objective-C

It has one answer as below:

/////// SuperClass.h
@interface SuperClass

@end

/////// SuperClass.m
@implementation SuperClass
- (void) protectedMethod
{}
@end

/////// SubClass.h
@interface SubClass : SuperClass
@end

/////// SubClass.m
@interface SubClass (Protected)
- (void) protectedMethod ;
@end

@implementation SubClass
- (void) callerOfProtectedMethod
{
  // this will not generate warning and call super's protectedMethod
  [self protectedMethod];
} 
@end

I tried it, and it indeed can call supler class method, working like 'protected'

My question is, why methods declared in (Protected) without implementation, but can be routed to super class's implementation? Could someone explain how it works?

Community
  • 1
  • 1
Wingzero
  • 9,644
  • 10
  • 39
  • 80
  • How is `[SuperClass protectedMethod]` *protected* in any way? – Droppy May 22 '15 at 08:52
  • You cannot simply call it externally, and it can be inherited, I think this is 'protected', probably compare to Java or C++ – Wingzero May 22 '15 at 09:03
  • What stops it from being called directly (other than a compiler warning)? – Droppy May 22 '15 at 09:14
  • I am asking why IT IS working, no warning. I am just don't know why I add a (Protected) category and declare it the same as super class, it will act as 'protected', meaning I don't have to implement it. – Wingzero May 22 '15 at 09:16
  • And I am telling you it isn't protected in the first place; simply hidden. – Droppy May 22 '15 at 09:19
  • all right, then, how it is hidden? Could you explain how it route to super class's method? – Wingzero May 22 '15 at 09:20
  • I know there is no real protected mechanism in Objective-C. But the sample provides a way to pretend to be protected, meaning you could not call it externally, and sub class can use it without implementation – Wingzero May 22 '15 at 09:21
  • Because Objective-C is a dynamic language unlike, say, C++, and the "routing" is done at runtime. The runtime can see that the class implements that method and successfully calls it. However it's not protected as the method can be called from anywhere, not just a subclass. – Droppy May 22 '15 at 09:22
  • it will be great if you could add more details and answer it then I can accept it as answer – Wingzero May 22 '15 at 09:23

1 Answers1

0

That method is not protected as any piece of code can call it, not just the subclass. It is merely hidden and you had to provide a declaration in the subclass in order to keep the compiler happy (however I suspect it's still complaining that [SubClass protectedMethod] has not been implemented and the code would still have worked without the declaration anyway).

The reason that code works is because the Objective-C runtime will send the message to the object regardless and if the method does not exist, then the classic unrecognized selector exception will be thrown.

So it works due to the dynamic nature of Objective-C where method calls are not bound by the linker and implemented via v-tables and the like. They are arbitrated at runtime. See this reference for more detail.

Droppy
  • 9,691
  • 1
  • 20
  • 27