Taking advantage of the multiple inheritance of protocols

Swift doesn't allow us to declare a class with multiple base classes or superclasses, so there is no support for multiple inheritance of classes. A subclass can inherit just from one class. However, a class can conform to one or more protocols. In addition, we can declare classes that inherit from a superclass and conform to one or more protocols. Thus, we can combine class-based inheritance with protocols.

We want the AngryCat class to conform to both the ComicCharacter and GameCharacter protocols. Thus, we want to use any AngryCat instance as both a comic character and a game character. In order to do so, we must change the class declaration and add the GameCharacter protocol to the list ...

Get Swift 3 ObjectOriented Programming - Second 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.