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 of protocols that the class conforms to and declare all the members included in this added protocol within the class.
The following lines show the new class declaration that specifies that the AngryCat
class conforms to both the ComicCharacter
and...