Reputation: 47
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! 🐻
Upvotes: -1
Views: 42
Reputation: 392
Polymorphism means "many forms" in IT-Context it just means, that one interface can have multiple forms. So overall everything you have described is polymorphism.It's just different forms. Most people from my daily interactions talk about the subclass overriding. But thats maybe just because of the Field I work in. But basically everything that allows to change behaviour without destroying the old one can be considerd polymorphism
Upvotes: 1