iOS

IOS Lesson 14 – Polymorphism in Swift

So with this knowledge bit, we are going to talk about a topic that will be going to have a lot of confusion while explaining to anyone and that is Polymorphism in Swift. Even though its implementation is very easy buts definition is something that always creates confusions. No worries, we have it all covered up! Polymorphism is often asked in interviews on Jobs and many times people get confused in explaining due to the definition being hard.

Polymorphism in Swift

Let’s go to Google. Type “Definition of Polymorphism” we can see the definition of polymorphism in its simplest form.

“The condition of appearing in several forms”

If we take a close look at the definition by Google we can get the idea about polymorphism that it should be something that can occur in several different forms. If we combine the generic concept with respect to programming it will have a similar logic something like this.

Any particular functionality that some methods want to do but every method uses its own way to achieve that functionality

You would probably be very confused right now but that’s ok. Let’s implement this “functionality” in the XCode Playground.

 

Open up the XCode and click on “Getting started with the playground”. Name this project anything you like. I’ve named it “14 – Polymorphism” as “14” is the number of lecture for this series. To explain Polymorphism, we are going to go with one of the simplest examples on the planet. And, that is Shape.

Let’s create a class called Shape. We are going to create a property to store “area” of type Double. We are going to make it optional so that it may or may not have a value. We are going to implement that class functionality by defining a function and name that function “CalculateArea”.

The logic behind polymorphism is that we create different classes that inherit from the base class(Shape) and whenever any class inherits from the shape, he will have the option to use the “CalculateArea” method but with its own way how it implement that method. You will see that in a moment.

Create two more classes one for Rectangle and one for Triangle. Implement the same method with the different way than each other.

Code:


class Rectangle : Shape{ // calculating the shape of rectangle by multiplying length into width
    override func CalculateArea(x: Double, y: Double) {
        Area = x * y
    }
}


class Triangle : Shape{
    override func CalculateArea(x: Double, y: Double) {
        Area = (x * y)/2
    }
}

Explanation:

What we have done is that we have created two different classes that inherit from the same base class(“Shape”) and calls the same method with its own implementation. Rectangle class will be going to multiply both parameters in order to get the area while the triangle class will divide the multiplication of x and y by 2 to get the area. And if you might have already noticed that the child classes can easily access the Area variable from the base class.

 

IOS lesson: Polymorphism in Swift

 

Conclusion:

In short, we got two different shapes here. Both of them are implementing the exact same function but the logic inside them is different and that is the polymorphism in Swift. Both of them are inheriting from the same base class and you will use this concept all the time.

So with this knowledge bit, We have covered Polymorphism in Swift and gone through how we can access methods and properties using the dot operator. In the next knowledge bit, we are going to look at polymorphism. You can get all the code from here.

you can read more about IOS tutorials from here.

Please Like, share and subscribe for more!

Click to comment

Most Popular

To Top