PDA

View Full Version : What does the SUPER in a subclass?




yrvaken2
Jan 7, 2009, 10:11 AM
If you create a UITableView Controller Subclass in Xcode, the viewDidLoad have the following code:


- (void)viewDidLoad {
[super viewDidLoad];
}


what does this "super" mean, when do you use that?

Thanks Anders



russellelly
Jan 7, 2009, 10:13 AM
It's sending a message to do the 'viewDidLoad' method in the superclass (ie UIView) as well as what you've written in that class (since writing a viewDidLoad method will over-ride what's in UIView).

SqueegyX
Jan 7, 2009, 11:00 AM
Lets say you have 2 class, a Parent and a Child. Child inherits from Parent. They have a method called greet which returns a string.

Here is what the parent method looks like:

-(NSString *)greet {
return @"Hello";
}

We want the child to learn from his parents. So we use super to say greet how Mommy would greet, but with our own little additions too.

// Inherits from Parent
-(NSString *)greet {
NSString *parentGreeting = [super greet];
return [parentGreeting stringByAppendingString:@", Mommy"]
}

So now Parent greets "Hello", and the Child greets "Hello, Mommy". Later on, if we change the parent's greet to return just "Hi", then both classes will be affected and you will have "Hi" and "Hi, Mommy".

super is used to call a method as defined by a superclass. It is used to access methods that have been overriden by subclasses so that the class can wrap its own code around a method that it's parent class implements. It's very handy if you are doing any sort of inheritance at all.

yrvaken2
Jan 7, 2009, 11:45 AM
Thanks for a good explanation!

dejo
Jan 7, 2009, 01:12 PM
what does this "super" mean, when do you use that?
If you don't understand the concept of superclasses and how they fit in, you may be best served stepping back and learning a bunch more about Object-Oriented Programming before you continue.