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

Get Objective-C® For Dummies® now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.