Implementing multiple protocols

A class can only inherit from a single class — this is the property of “single inheritance”. In contrast, a class can adopt as many protocols as you’d like!

Suppose instead of creating a WheeledVehicle protocol that inherits from Vehicle, you made Wheeled its own protocol:

protocol Wheeled {

var numberOfWheels: Int { get } var wheelSize: Double { get set }

}

class Bike: Vehicle, Wheeled {

// Implement both Vehicle and Wheeled

}

Protocols support “multiple conformance”, so you can apply any number of protocols to types you define. In the example above, the Bike class now has to implement all members defined in the Vehicle and Wheeled protocols.

results matching ""

    No results matching ""