Polymorphism is a fundamental object-oriented programming concept; however, it's also very important in POP. It is imperative to understand it and apply it correctly. In this video,watch how to build a demo that relies on protocols and polymorphism.
- [Instructor] Polymorphism is a fundamental object oriented programming concept, yet it's also essential to POP. 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. Let's open it by going to the Desktop, Exercise Files, chapter two, 0204, and Start.
I'm going to open the Polymorphism playground. The Shape protocol defines the read only getable area property. This is the only property requirement that conforming types must implement. We'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. We can't instantiate the shape protocol directly, yet we can take advantage of the polymorphic behavior and assign any of the concrete types to the shape variable.
For example, I'm going to assign a rectangle instance first. Let's say 10 by 20. I'm going to print out the shape's area. Next I'm going to assign a circle of radius, say, 100. And we can print out this shape's area, as well. Note that we've been using the same shape variable.
But first I assign it a rectangle, and now a circle. Next I'm going to assign a rhombus. Let's make it 20 by say 50. And again, we print the shape's area. Swiss primary collections can only work with well-defined types of values. This means that we can't insert random types into a collection.
Shape provides the common interface for all conforming types. This let's us define an array of shape types just like this. Var shapes equals, I'm going to use the shorthand form for array, and provide the type here. This is how we can construct an empty array of shapes. Now let's append a couple of instances. First I'm going to create a square equals square, make it 50, and using the array append method, I'm going to add the square.
Next, I'm going to create a rectangle. Let's make it, say, 20 by 100. And again, I'm going to call append and add it to our shapes array. Let's add another one, a circle, of radius 400, and append it to our array.
We could basically add any instances of the conforming types to this array. Now polymorphism allows us to access the common functionality defined in the shape protocol. We don't need to know the actual type. So I can write a for in loop like this, for shape in shapes. And then print each shape's area. We can see in the console, the area of each shape that was added to the shape's array.
Polymorphism is useful when we need to access and use types through a common interface. This allows us to treat instances of similar types without having to keep track of their exact type.
- Comparing object-oriented programming with protocol-oriented programming
- Methods and class-bound protocols
- Adopting a protocol
- Declaring asynchronous behavior
- Preparing and implementing fallback logic
- Implementing an app using protocol-oriented programming