0

After some research, I can't find anything on super classes, so I decided to ask here.

Example

@interface superClass : CCSprite
@end

@interface subClass : superClass
@end

How do the two above examples relate to each other? Also, I was shown that you could add a method the the superClass, and then use it in the subClass (how?)?

akuritsu
  • 440
  • 1
  • 4
  • 18
  • You'll find this helpful: https://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/ObjectiveC/Chapters/ocDefiningClasses.html – CodeSmile May 21 '12 at 21:32

1 Answers1

1

CCSprite is the super class of superClass superClass is the super class of subClass

there is two ways of using method in super class, for example

@interface superClass : CCSprite
- (void)doSomething;
- (id)somethingElse;
@end

@implement superClass
- (void)doSomething {
    NSLog( @"do something in super class" );
}
- (id)somethingElse {
    return @"something else in super class";
}
@end

@interface subClass : superClass
- (void)doSomethingTwice;
@end
@implement subClass
- (void)doSomethingTwice {
    [self doSomething];
    [self doSomething];
}
- (id)somethingElse {
    id fromSuper = [super somethingElse];
    return @"something else in sub class";
}
@end

subClass sub = [[subClass alloc] init];
[sub doSomethingTwice];   // this will call `doSomething` implemented is superClass twice
NSLog([sub somethingElse]); // this will call `somethingElse` in super class but return "something else in sub class" because it override it

basically you can just call the method implemented in super class on instance of sub class

and you can override the method in sub class to do something different and/or using [super methodName] to call the method implementation in super class

Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
  • I'm guessing that the subClass and superClass would be two seperate files, like: subClass.h, superClass.h, with their .m files too? – akuritsu May 23 '12 at 03:13