O'Reilly logo

Learning iOS Development: A Hands-on Guide to the Fundamentals of iOS Programming by Erica Sadun, Rod Strougo, Maurice Sharp

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

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

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