Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Now that Car has accessors, let's take advantage of them. Instead of the stock engine and tires, we'll implement variations on these parts. We'll use inheritance to make the new kinds of engines and tires, and then use Car's accessors (that's composition) to give the car its new moving pieces. The code for this program can be found in the 05.03 CarParts-2 folder.
First is a new kind of engine, a Slant6 (if you prefer a V8 or a ThreeFiftyOneWindsor, go for it).
@interface Slant6 : Engine
@end // Slant6
@implementation Slant6
- (NSString *) description
{
return (@"I am a slant-6. VROOOM!");
} // description
@end // Slant6
A Slant6 is a kind of engine, so it makes sense for us to subclass Engine. Remember that inheritance sets up a relationship that allows us to pass subclasses (Slant6) where the super-class (Engine) is expected. Because Car takes an argument of type Engine for the setEngine: method, we can safely pass in a Slant6.