Let's say you have 2 classes, a parent and a child. The child inherits from the parent. They have a greet method that returns a string.
Here's what the parent method looks like:
the code:
-(NSString *)greet { return @"Hello"; }
We want the child to learn from his parents. Therefore, we use super to welcome how mom will welcome, but also with our own little additions.
code: // Inherited from parent
-(NSString *)greet { NSString *parentGreeting = [super greet]; return [parentGreeting stringByAppendingString:@", Mommy"] }
So, now Parent welcomes "Hello", and Child welcomes "Hello, Mommy". Later, if we change the parent hello to only return “Hello,” then both classes will be affected, and you will have “Hello” and “Hello, mom.”
super is used to call the method defined by the superclass. It is used to access methods that have been overridden by subclasses so that the class can wrap its own code around a method that implements its parent class. This is very convenient if you don’t inherit anything at all.
suthar Oct 21 '14 at 11:33 2014-10-21 11:33
source share