-1

I have been programming for a few years now and I have always had trouble understanding the concept of polymorphism. I have had many different professors try to explain to me the concept in their own way but for-whatever reason, none of their explanations stick.

From what I have researched, in my own words: Polymorphism is basically the ability of a method or object to behave in different ways depending on the context of the program.

Am I right in this definition or is there more to it?

Any help would be appreciated! Thank you!

user3566859
  • 49
  • 1
  • 4

1 Answers1

0

Exactly,

Since you are trying to understand polymorphism, I think you are new to oop.

In oop we use interfaces, we implement them to our classes.

For example we have interface shape

And classes, triangle, rectangle, circle, we can implement this interface to each class.

The interface is like prototyp of class which is prototyp to object, right?

This interface has methods like giveMeArea and in every class is this method implemented, but in different way for rectangle, circle or triangle.

Since them inherit the interface, we can put them to object made of this interface, and without knowing what is the shape, we can call giveMeArea and we will get right results.

You can have like collection shape full of rectangles, circles, triangle, and you can just like that call shape.giveMeArea even if you dont know if you call it for rectangle or circle, you just call it for the shape, and polymorphism will give you right areas.

Hope I helped you.

Kerman
  • 29
  • 3