properties and subclasses

  • I have a situation where I've created a subclass that overrides a number of synthesized property accessors in its superclass. What I'm wondering is whether the overridden methods will get called when using dot syntax, or if this will just return the (inherited) synthesized properties?

    So the "Thing" class has a property:

    @property thingProp* aProp;

    which is synthesized:

    @synthesize aProp;

    then the subclass "SubclassThing" has a hand-coded accessor to modify precisely what aProp returns:

    - (thingProp*)aProp
    {
    thingProp* somethingDifferent = notTheSameAsSuper;
    return somethingDifferent;
    }

    With this setup, when I call mySubclassThing.aProp, do I get the synthesized property (inherited) or the overridden method return? That is, do I have to deliberately call [mySubclassThing aProp] to get the method return (i.e., not using dot syntax)? Or, saying it another way, does a subclass' hand-coded accessor override the superclass' synthesized accessor?

    Strange question, I suppose, but it isn't totally clear to me what actually happens.

    J.

    James B Maxwell
    Composer/Doctoral Candidate
    School for the Contemporary Arts (SCA)
    School for Interactive Arts + Technology (SIAT)
    Simon Fraser University
  • On Jul 5, 2012, at 8:25 AM, James Maxwell wrote:

    > I have a situation where I've created a subclass that overrides a number of synthesized property accessors in its superclass. What I'm wondering is whether the overridden methods will get called when using dot syntax, or if this will just return the (inherited) synthesized properties?

    The overridden methods. Properties are just syntactic shorthand for getter and setter methods, so the usual Objective-C rules of inheritance work. (In Objective-C all methods are 'virtual' in C++ terminology; if a method is overridden in a subclass, the only way to call the inherited method is via 'super' from inside a subclass method.)

    —Jens
  • On 06/07/2012, at 1:25 AM, James Maxwell wrote:

    > With this setup, when I call mySubclassThing.aProp, do I get the synthesized property (inherited) or the overridden method return? That is, do I have to deliberately call [mySubclassThing aProp] to get the method return (i.e., not using dot syntax)? Or, saying it another way, does a subclass' hand-coded accessor override the superclass' synthesized accessor?
    >
    > Strange question, I suppose, but it isn't totally clear to me what actually happens.

    It will work fine.

    @synthesize is really just a bit of syntactic sugar to save you hand-writing -foo and setFoo:, so overrides of those work normally whether synthesized or not, and the dot syntax is just syntactic sugar for the usual square brackets notation (which itself is shorthand for a call to objc_msg_send).

    It'll work fine.

    --Graham
  • Okay, thanks. That seems reasonable, but I wanted to double-check. I remember reading some discussion a while ago that seemed to suggest that dot syntax was only for calling synthesized properties, not methods… Though precisely what a synthesized property would be, other than an accessor method (operative word being "method"), was never really explained! ;-)

    Thanks again.

    J.

    On 2012-07-05, at 8:40 AM, Jens Alfke wrote:

    >
    > On Jul 5, 2012, at 8:25 AM, James Maxwell wrote:
    >
    >> I have a situation where I've created a subclass that overrides a number of synthesized property accessors in its superclass. What I'm wondering is whether the overridden methods will get called when using dot syntax, or if this will just return the (inherited) synthesized properties?
    >
    > The overridden methods. Properties are just syntactic shorthand for getter and setter methods, so the usual Objective-C rules of inheritance work. (In Objective-C all methods are 'virtual' in C++ terminology; if a method is overridden in a subclass, the only way to call the inherited method is via 'super' from inside a subclass method.)
    >
    > —Jens

    James B Maxwell
    Composer/Doctoral Candidate
    School for the Contemporary Arts (SCA)
    School for Interactive Arts + Technology (SIAT)
    Simon Fraser University
  • On Jul 5, 2012, at 9:56 AM, James Maxwell wrote:

    > Okay, thanks. That seems reasonable, but I wanted to double-check. I remember reading some discussion a while ago that seemed to suggest that dot syntax was only for calling synthesized properties, not methods… Though precisely what a synthesized property would be, other than an accessor method (operative word being "method"), was never really explained! ;-)
    >
    > Thanks again.
    >
    > J.

    The discussion may have been referring to misuse, such as calling "obj.retain" (which I believe the compiler warns about). But since dot-syntax is just an alternate syntax for calling methods, you can use it on methods in older code that are properties conceptually but aren't explicitly declared such, like NSArray's -count method.

    Preston
previous month july 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today