5

When I define a new class inheriting from NSObject:

@interface Photo : NSObject
{
    NSString* caption;
    NSString* photographer;
}

@property NSString* caption;
@property NSString* photographer;

@end

are all the class methods (like alloc) in NSObject inherited by the new class Photo?

Charles
  • 50,943
  • 13
  • 104
  • 142
ray
  • 145
  • 9

1 Answers1

5

Yes, Photo can use any method/property/ivar/etc (except for those iVars declared @private) of a NSObject when you subclass NSObject:

Photo *myPhoto;
myPhoto = [[Photo alloc] init];
// ... Do some myPhoto stuff ...
NSLog(@"Photo object: %@", myPhoto);
NSLog(@"Photo description: %@", [myPhoto description]);
NSLog(@"Photo caption: %@", [myPhoto caption]);
NSLog(@"Photo photographer: %@", [myPhoto photographer]);

More about @private -> SO Question: what-does-private-mean-in-objective-c

NSObject Class Reference

Community
  • 1
  • 1
chown
  • 51,908
  • 16
  • 134
  • 170
  • 1
    Might want to modify your answer: neither methods nor properties have access levels (e.g. `@private`.) And even ivars (which do have access levels) can be accessed via the Objective-C runtime. – Jonathan Grynspan Oct 26 '11 at 02:41
  • The `@private` compiler directive only applies to instance variables, not methods. Also, the dot syntax shouldn't be used to invoke anything other than accessor methods, and `NSObject` doesn't have any accessors. From the Objective-C Programming Guide: "Objective-C also provides a dot (.) operator that offers a compact and convenient syntax for invoking an object’s accessor methods." http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW1 – jlehr Oct 26 '11 at 02:44
  • I don't believe this answers the question; all the methods in the example are instance methods, but the question is specifically about _class_ methods. – big_m Nov 02 '15 at 13:40
  • That said, it is true that class methods are inherited, as the class's metaclass is a subclass of the superclass's metaclass. http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html – big_m Nov 02 '15 at 13:50