Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Blog Reader iPhone App Data Modeling Designated Initializers and Convenience Constructors

What is the need for super?

What is the need for [super init] to create an instance of self? I dont understand why we need to go to the parent class to initialize when the subclass has access to all of the parent class's methods anyways. Is it because we dont know which type of subclass will be using the init method (we are using id), so to be safe we initialize with the parent class (here its NSObject)?
Thx

2 Answers

Stone Preston
Stone Preston
42,016 Points

because if you override a super classes method (like init) you want to build on top of that method and make sure it does what the super class method did in addition to the new stuff you want to add, not redefine it completely. You want it to do everything the super classes method does in addition to something else, so you call [super methodName] to make sure it does what the super class method did, and then add additional stuff to it after that.

so if we didnt call the super class, we could end up redefining a method that has already been redefined once before in the subclass and the new result could cause unintended havoc?

Stone Preston
Stone Preston
42,016 Points

pretty much. The super class method might do something that was necessary and if you didnt call the super method first, it wouldnt get done and problems would result