I’m studying up on archiving data, and I realize the standard collection classes provide easy archival, however I want to archive an NSMutableArray. So I read that since NSMutableArray “is a” NSArray because it is a subclass, then this is fine.
But that idea stumps me, and I realize this is a basic concept of Objective-C so I want to make sure I get it right.
Suppose you have a class “automobile” with a subclass “jeep.” Now based on the above, you can say this:
automobileInstance=jeepInstance;
…because a jeep is a subclass of automobile.
But this seems backwards to me. I would think you should instead say that a subclass instance can be equal to its superclass instance:
jeep = auto
Because after all a jeep “is a” automobile, but not all automobiles are jeeps.
So why then can you do this:
NSArray*newArray=nsmutableArrayInstance;
It’s the same idea as saying:
automobile=jeep; in my example.
Your subclass might have added many new instance variables, for example, but if you set it equal to a superclass, those iVars are “lost” or at least the super class doesn’t recognize them, so it isn’t truly capturing the object, it would seem.
Going the other way, saying the sublass instance = superclass instance makes sense, since everything in the superclass is in the subclass; you just don’t get values for the extra iVars that are in the subclass but not in the superclass, they might be set to default zero, but at least they exist.
Think of it this way: all jeeps are automobiles. Not all automobiles are jeeps. So you can say:
Automobile a = jeepInstance; // A jeep is always an automobile
But not:
Jeep j = autoInstance; // fails – autoInstance might be a Mazda, instead.