Welcome, Guest. Please login or register.
Did you miss your activation email?
February 23, 2017, 02:20:41 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ General Discussion
| | |-+ Recursive init
Pages: [1] Go Down
Print
Author Topic: Recursive init (Read 4978 times)
jgross
Newbie
*
Posts: 8


on: May 02, 2013, 11:52:41 PM

Is there a way to call the init method of a superclass that is not the immediate parent class?  I'm trying to setup three classes each of which is a subclass of the previsous (ClassA : ClassB : ClassC).   I've set this up this way because I want each class to be able to call the methods of it's parent (or grand-parent).  I would like to override the init method for each class, so that each calls the base NSObect method init and then set's values for various instance variables of each class.  The problem I'm running into is that the statement:
Code: (Objective-C)
self = [super init]
only works at the highest level of recurision.  Each of the two levels of subclasses try to call the init method of their immediate parent and choke on the instance variables that aren't applicable to them.  Is this the wrong way to setup the relationships of these classes?  I think that in all three cases, if I didn't want to call the methods of the second class from within the methods of the first I would not subclass them to each other, but just make them all subclasses of NSObject.  Have I gone about this the wrong way?

Jim
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



Reply #1 on: May 03, 2013, 11:17:54 AM

The way the inheritance chain is defined, there should be no problem with each init making a call to super.   A class should be responsible for allocating (if necessary) it's own instance variables, and not its inherited ones.   The latter are done by the super init method(s).

So you shouldn't  need or want to bypass an init method from a parent class.

Steve
Logged
Daigoryu
Newbie
*
Posts: 1


Email
Reply #2 on: July 28, 2016, 04:08:28 AM

The information you presented it affect me quite a lot of research. What is difficult to read.
Logged

maxbet

Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.