• 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
Re: Property inheritance
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Property inheritance


  • Subject: Re: Property inheritance
  • From: Ken Thomases <email@hidden>
  • Date: Wed, 04 Jul 2012 10:46:59 -0500

On Jul 4, 2012, at 9:32 AM, William Squires wrote:

> Here's my class graph:
>
> Shape (base class - inherits only from NSObject)
> LineShape : Shape
> RectangleShape : Shape
> SquareShape : RectangleShape

Just so you know, this is the archetypal example of a violation of the Liskov substitution principle. <https://en.wikipedia.org/wiki/Liskov_substitution_principle#A_typical_violation>  The problem is that you can't pass a SquareShape to code which is expecting a RectangleShape because such code is entitled to do things like set rectSize to non-square dimensions.

> If a subclass (SquareShape) redefines the @property in a superclass as readwrite, are there any 'gotcha's I need to watch out for?

No.  This is an intended use case. <https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW19>


> @implementation SquareShape
>
> #pragma mark - Non-synthesized accessors
>
> -(CGFloat)area
> {
> return (self.rectSize.width * self.rectSize.height);
> }
> …
> -(CGFloat)perimeter
> {
> return (self.rectSize.width * 2.0 + self.rectSize.height * 2.0);
> }

It shouldn't be necessary to re-implement the getters.  If the compiler complains – I don't think it should – then you can quiet it with "@dynamic area, perimeter;".

Regards,
Ken


_______________________________________________

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


References: 
 >Property inheritance (From: William Squires <email@hidden>)

  • Prev by Date: Re: Dynamic modification of text search results
  • Next by Date: Re: Prevent Quarantine message on helper app
  • Previous by thread: Property inheritance
  • Next by thread: Binding NSTableView to NSSet
  • Index(es):
    • Date
    • Thread