Chris
Chris

Reputation: 13

Can I change the super of a class?

Is it somehow possible to choose the super of a class (preferably in the alloc or init method) so my class inherits from something else?

Upvotes: 0

Views: 948

Answers (2)

No, you can’t. And: yes, you can.

A class(!) is a relative static thing. It is “hard wired” to its superclass. This is important as there must be some thing, knowing how to handle the allocated memory and other things.

Something different is an instance. You get an instance of some class by “asking” its class for one. (And, usually, after getting one you ask for initialization and so on.)

At this point the usual behaviour can be “broken”. But, as I think, this is nothing to do for beginners, you should be experienced when doing such tings! Why? Read the stuff, Apple is saying about this. And see, that even Apple does such things! Have a look at class clusters like NSNumber: Cocoa Fundamentals Guide (Can’t post more than this one, but you surely find NSNumber-documentation without my help.)

Upvotes: 0

jlehr
jlehr

Reputation: 15597

You can do that in -init by creating an instance of the desired target class and resetting self. Be sure to send a -release message to the previous instance if you do that, though.

Upvotes: 3

Related Questions