I’m really confused about what polymorphism in OOP actually means. Everyone explains it differently, and I’m not sure which definition is correct:
In some places, polymorphism is explained as dynamic polymorphism:
Subclasses override methods from the parent class, and at runtime, the correct method is chosen.
Example: dog.MakeSound(); → "Woof!" cat.MakeSound(); → "Miau!"
In other places, polymorphism is subtyping:
An object of the child class can be used wherever the parent class is expected.
Example: Animal[] animals = Animal[] {new Dog(), new Cat()};
And then there’s static polymorphism:
Overloaded methods (same name, different parameters) resolved at compile time.
Example: void Add(int a, int b) and void Add(double a, double b).
My question: What’s the actual definition of polymorphism? Does it include all of these, or is only one part correct? I’m totally confused and would really appreciate your help!
Thanks in advance! 🐻