Re: Subclassing/Override question
Re: Subclassing/Override question
- Subject: Re: Subclassing/Override question
- From: Peter Tomaselli <email@hidden>
- Date: Mon, 04 Jan 2016 14:57:04 -0500
Method return types are covariant though, yes? So you should always be able
to return a more-derived type from a method that is declared to return a
less-derived type. If I read your example right, ClassA is "Animal" and
ClassB is "Cat" (in covariance-blog-post lingo). So that is legal, unless
I'm missing something.
Perhaps the issue is that the @interface for your overriding class (the one
that implements the override of "propA") does not redeclare that method as
returning (ClassB *)? I am away from Xcode right now (and as such have
immediately begun to doubt everything I know about programming and the
universe in general), but I reckon if you don't declare that in the
overridden @interface then other public callers are just going to dumbly
see the superclass @interface. But that's not a variance problem (again,
assuming I'm not messing something up here), that's just that you need to
declare it.
Peter
On Mon, Jan 4, 2016 at 11:40 AM, Jeff Kelley <email@hidden> wrote:
> Hi Dave,
>
> For myClassA, you will always have to cast the return value to
> myClassB if you know that’s what will be returned. For a great blog post on
> the subject, I give you Mike Ash:
>
>
> https://mikeash.com/pyblog/friday-qa-2015-11-20-covariance-and-contravariance.html
>
>
> Jeff Kelley
>
> email@hidden | @SlaunchaMan <https://twitter.com/SlaunchaMan> |
> jeffkelley.org <http://jeffkelley.org/>
> > On Jan 4, 2016, at 11:24 AM, Dave <email@hidden> wrote:
> >
> > Hi,
> >
> > In Objective-C, is it possible to override a property and have it return
> a different type to the base class?
> >
> > I have a base class with the following property defined:
> >
> > @property (nonatomic,retain) ClassA*
> propA;
> >
> > and the following getter:
> >
> >
> > -(ClassA*) propA
> > {
> > return someValueOfClassA;
> > }
> >
> >
> > I’d like to override this in my subclass, as so:
> >
> > -(ClassB*) propA
> > {
> > ClassA* myClassA;
> > ClassB* myClassB;
> >
> > myClassA = super.propA;
> > myClassB = [[ClassB alloc] initWithClassA:myClassA]; //ClassB is a
> subclass of Class A
> >
> > return myClassB;
> > }
> >
> > I can do this ok, but when I access the property:
> >
> > // myClassA is already setup OK
> > ClassB* myClassB;
> >
> > myClassB = myClassA.propA; //Warning on this line
> saying return type is not ClassB
> >
> > and I have to coerce the value to be ClassB to get rid of the warning.
> Is there any way to avoid this?
> >
> > Thanks a lot
> > Dave
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden