What is polymorphism?

Definition: Allow you to call a method of a class without knowing the exact type of the class

It is crucial to understand what a polymorphism is as this is the most common interview questions where you will be asked to explain what a polymorphism is.

Let’s try implementing it on the code and understand the concept of polymorphism.

The first step to this is to go to File > New > Playground and give it a name

After creating your own playground, your screen should look like this. We wont be needing

In the code section, I’ve created an Animal class with a makeNoise() that returns noise. I have created several subclasses such as Dog and Cat to inherit the makeNoise() but I do not want Dog and Cat return noise, so I’ve decided to override the function by implementing a different noise whereas Dog returns woof and Cat returns meow.

With all the code ready, I need to test it out and call them so I create a variable and start calling the function within each class which return different noise. With this, I hope you are able to experiment your own code and have a greater understanding on Polymorphism.

  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

One thought on “What is polymorphism?

Leave a Reply

Your email address will not be published. Required fields are marked *