0

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?

4
  • Why don't you just give an id datatype to x and y? Commented Dec 3, 2010 at 5:19
  • @Dave Delong. I did try it, I just wanted to know the options around it. I know one can redefine methods in both classes. I was just looking for a better explanation on why I get the duplicate error when I change the types of the instance variables. Commented Dec 3, 2010 at 5:34
  • @Jon Limjap: Because id is a pointer data type and x and y are primitives. Commented Dec 3, 2010 at 9:01
  • @JeremyP which can easily be wrapped in NSNumber... maybe that's the correct type for this one :) Commented Dec 6, 2010 at 2:51

2 Answers 2

1

I'm pretty sure you can't do that for two reasons:

  1. You can't repeat instance variables (int x, double x).
  2. It's pretty sketchy to inherit from one class, but then want to change the type of its instance variables.

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 NSNumber family, 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.

Sign up to request clarification or add additional context in comments.

4 Comments

I am just getting into obj-c, and since you can redefine instance variables for extending classes, I was just wondering if you could also change their types.
There are no members in Objective-C; just instance variables. :)
I wasn't aware you could redefine instance variables when extending classes. I wonder whether you mean that you can redefine instances methods? That's definitely true. The deal with Objective-C classes, is that they're really just C structs underneath it all, and you can't duplicate instance variables of structs. (I hope I'm not leading you astray).
@bbum Thanks for the terminology correction! I've fixed my answer. I was under the impression that member was just a more general term, but I defer to your wisdom!
1

No. You get a compile-time warning of "Duplicate member 'x'" and "Duplicate member 'y'".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.