Inheritance and Subclassing
In Objective-C, each new class is derived from a class that already exists. The Car
class described in Listings 2-3 through 2-7 inherits from NSObject
, the root class of the Objective-C class tree. Each subclass adds or modifies state and behavior that it inherits from its parent, also called its “superclass.” The Car
class adds several instance variables and methods to the vanilla NSObject
it inherits from.
The HybridCar
class inherits from Car
and adds functionality to calculate the distance the car can travel until empty, based on the MPG that the hybrid car is capable of. Listings 2-9 and 2-10 show one possible way to implement the HybridCar
class. The sample code for this chapter contains a project with the code ...
Get Learning iOS Development: A Hands-on Guide to the Fundamentals of iOS Programming 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.