Skip to Main Content
Objective-C® For Dummies®
book

Objective-C® For Dummies®

by Neal Goldstein
October 2009
Beginner content levelBeginner
456 pages
8h 59m
English
For Dummies
Content preview from Objective-C® For Dummies®

Chapter 16. Extending the Behavior of Objects

In This Chapter

  • Using delegation to implement a new transaction

  • Defining formal and informal protocols

  • Using categories to extend a class

In your application so far, you have two kinds of transaction objects, a CashTransaction and a CreditCardTransaction. As I was field testing the application, sitting in a bar (bars in Italy serve coffee, so don't get too excited) on the Grand Canal in Venice, I needed some euros, so I went to the ATM machine.

It dawned on me that since this was not my own bank's ATM, I had to pay a $2.00 transaction fee. I realized I need to add a new type of transaction — ATM.

In Chapter 11, you learn to use inheritance to create subclasses such as CashTransaction and CreditCardTransaction to implement generic functionality that was defined as a superclass, such as the spend functionality in the Transaction class. I also mentioned that you could also use inheritance to add new functionality, new methods, and new instance variables to a subclass.

So it would make sense to use inheritance to create a new subclass. If I did that it would also mean, thanks to polymorphism, the only changes I would have to make to my program, besides defining the new class, would be to add a new method to Destination (in addition to the existing spendCash: and chargeCreditCard: methods) — useATM: to create the new ATM transaction.

As you start to work with the UIKit and AppKit frameworks, you will be using inheritance to extend the behavior of ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Objective-C

Objective-C

Jiva DeVoe
Cocoa in a Nutshell

Cocoa in a Nutshell

Michael Beam, James Duncan Davidson

Publisher Resources

ISBN: 9780470522752Purchase book