Is it possible to handle a class with different instance variable types? Let’s say I have a class that has two int instance variables, let’s call them:
@interface ClassA: NSObject {
int x;
int y;
}
@end
At the same time I want to extend it giving it the possibility to handle a different type on such instance variables like so:
@interface ClassB: ClassA {
double x;
double y;
}
Is this even possible?
I’m pretty sure you can’t do that for two reasons:
int x, double x).If you don’t want to make separate classes for these use-cases, perhaps an abstract superclass would work. If I more about what you’re trying to solve, I’d be able to be a bit more helpful in this area.
What it seems like you really want is parametric polymorphism, which Objective-C does not support. Certain Cocoa classes, like the
NSNumberfamily, use an abstract superclass with many concrete subclasses, presumably with different instance variable layouts (like you describe in your question). Then, logic is divided appropriately between the abstract and the specific. This is Cocoa’s Class Cluster design pattern, which is a sort of weakened, ad-hoc answer to the parametric polymorphism of languages like C++ and Haskell.