From the course: Swift 5: Protocol-Oriented Programming

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Understanding polymorphism

Understanding polymorphism

From the course: Swift 5: Protocol-Oriented Programming

Start my 1-month free trial

Understanding polymorphism

- Polymorphism is a fundamental object oriented programming concept and it's also essential to protocol oriented programming. Polymorphism comes from Greek and means many forms in programming. It denotes the ability to provide a single interface to different types. To illustrate the concept, I went ahead and created a playground project. You can find it in the exercise files for their chapter one or 105 and begin. Let's have a look at the shapes playground. The shape protocol defines the read-only area properly. This is the only property requirement that conforming types must implement. I've created several concrete shapes that conform to the shape protocol, square, rectangle, rhombus, and circle. Now let's define a variable of type shape VAR shape of type shape. We can't instantiate the shape protocol directly. However, we can take advantage of the polymorphic behavior and assign any of the concrete types to the shape…

Contents