Extension through Inheritance: Adding New Methods

Inheritance often is used to extend a class. As an example, let’s assume that you’ve just been assigned the task of developing some classes to work with 2D graphical objects such as rectangles, circles, and triangles. For now, we’ll worry about just rectangles. Let’s go back to exercise 7 from Chapter 4, “Data Types and Expressions,” and start with the @interface section from that example:

@interface Rectangle: NSObject@property int width, height;-(int)  area;-(int)  perimeter;@end

You’ll have synthesized methods to set the rectangle’s width and height and to return those values, and your own methods to calculate its area and perimeter. Let’s add a method that will ...

Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.