Quick polymorphism and subclassing question
Quick polymorphism and subclassing question
- Subject: Quick polymorphism and subclassing question
- From: Greg Best <email@hidden>
- Date: Sun, 6 Mar 2005 22:59:53 -0800
I think you have two options depending on what you're using the
returned object for...
Because KBTextView is a subclass of NSTextView, it can function as its
superclass, so I believe
-(NSTextView*)textView;
can return your KBTextView instance. The catch is that no one
receiving it will know there's anything different about it, and
depending on how you've subclassed it it might behave differently than
expected.
Alternately you could use your second method:
- (KBTextView *)kbTextView;
which ensures that the caller gets what they asked for. You could also
provide both...
The first method you show is probably legal, but I think it's the least
preferred.
I'm sure someone here will have an answer based more on CS theory...
Cheers--
Greg
Hello,
A very basic question, I fear, but I would be very
grateful if someone could clear this one up for me by
explaining to me the proper way of doing things:
Suppose I subclass NSTextView and call my subclass
KBTextView. Then I use this subclassed text view as an
instance variable in another class and want to provide
an accessor method to it. What would be the proper way
of creating a name for this method? I have been doing
this:
- (KBTextView *)textView;
However, NSTextStorage has a -(NSTextView *)textView
method. Is this a violation of the principles of
polymorphism? Should all methods with the same name
have exactly the same return type? ie. Would it be
better to have an accessor method with a different
name to indicate the different return type, such as:
- (KBTextView *)kbTextView;
This is probably very obvious, but I would be very
thankful if someone could enlighten me.
Many thanks,
Keith
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden