Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
December 17, 2018, 01:13:41 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
Author Topic: Recursive init  (Read 7701 times)
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?

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.

Posts: 1

« 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.

Pages: [1]   Go Up
Jump to:  

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 ゥ 2009 All rights reserved.