O'Reilly logo

Programming in Objective-C, Sixth Edition by Stephen G. Kochan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required