Overriding Methods

We noted earlier in this chapter that you can’t remove or subtract methods through inheritance. However, you can change the definition of an inherited method by overriding it.

Returning to your two classes, ClassA and ClassB, assume that you want to write your own initVar method for ClassB. You already know that ClassB will inherit the initVar method defined in ClassA, but can you make a new method with the same name to replace the inherited method? The answer is yes, and you do so simply by defining a new method with the same name. A method defined with the same name as that of a parent class replaces, or overrides, the inherited definition. Your new method must have the same return type and take the same number and type ...

Get Programming in Objective-C, Sixth Edition 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.