Re: Property inheritance
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