• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Subclassing/Override question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Subclassing/Override question


  • Subject: Subclassing/Override question
  • From: Dave <email@hidden>
  • Date: Mon, 04 Jan 2016 16:24:47 +0000

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


  • Follow-Ups:
    • Re: Subclassing/Override question
      • From: Jens Alfke <email@hidden>
    • Re: Subclassing/Override question
      • From: Jeff Kelley <email@hidden>
  • Prev by Date: Re: Any way to intercept the URL-opening mechanism on OS X?
  • Next by Date: Re: Subclassing/Override question
  • Previous by thread: Re: Any way to intercept the URL-opening mechanism on OS X?
  • Next by thread: Re: Subclassing/Override question
  • Index(es):
    • Date
    • Thread