6

I expected [super class] to return the superclass's class, however I found, using this code that it returns this class's class.

Code

NSLogObject([self class]);
NSLogObject([super class]);
NSLogObject([self superclass]);
NSLogBool([self class] == [super class]);

Output

[self class]: MainMenuScene
[super class]: MainMenuScene
[self superclass]: CCScene
[self class] == [super class]:[YES]

Can somebody explain why this happens please?. I expect it to return the same value as [self superclass].

Macros:
-------
#define NSLogBool(i)   NSLog(@"%s:[%@]", #i, (i) ? @"YES" : @"NO")
#define NSLogObject(o) NSLog(@"%s:[%@]", #o, o)
James Webster
  • 31,873
  • 11
  • 70
  • 114

3 Answers3

6

[super class] calls the super method on the current instance (i.e. self). If self had an overridden version, then it would be called and it would look different. Since you don't override it, calling [self class] is the same as calling [super class].

mprivat
  • 21,582
  • 4
  • 54
  • 64
  • Thanks. I'm just being dozy and assuming a method called "class" should somehow act differently to the rest of oop! – James Webster Aug 06 '12 at 12:05
  • I admit I fell into the same trap for a second while reading your question. No worries. – mprivat Aug 06 '12 at 14:57
  • 1
    even if you didn't override it, it is possible that `[self class]` is not the same as `[super class]`, if `self` is an instance of a subclass which overrides the method; then it would call that subclass's implementation – newacct Aug 06 '12 at 17:58
  • 1
    I don't really understand this answer. There is no `super` method. `super` is a keyword. – Rian Rizvi Aug 02 '13 at 22:24
  • @RiazRizvi: What they meant was "the superclass's implementation of the method" without saying it in so many words. – newacct Aug 05 '13 at 05:31
3

super refers to the method implementation in the superclass. If you don't override the method in your class, the implementation in your class and its superclass is the same.

Both [super class] and [self class] call the same method, defined on NSObject.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • 1
    even if you didn't override it, it is possible that `[self class]` is not the same as `[super class]`, if `self` is an instance of a subclass which overrides the method; then it would call that subclass's implementation – newacct Aug 06 '12 at 17:58
0

While the answers by mprivat and Sulthan are technically correct, the current case is a bit more complicated (and interesting):

Considering the implementation of the call to the method. It uses (of course) the object pointer. An object pointer is a pointer to a struct that has as its first element (isa) is a pointer to the objects class. The class methods (that is, as mprivat and Sulthan has stated correctly, the very same in both cases) follows this pointer to determine the class, i.e., always the class of the calling object.

As a result, if A is the superclass of B, both calls [self class] and [super class] called by an instance of B, return the class of B (not of A, what one may expect because of a missing override!).

Matthias
  • 8,018
  • 2
  • 27
  • 53