-1

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! 🐻

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

So, you mean polymorphism is a broad concept, and many programmers understand polymorphism as subclasses overriding the methods of the parent class?
Basically yes. The first way how i learned polymorphism back in vocational school was the good ol Animal concept: Dog has Animal Interface, animal interface has speak method. Dog.Speak and Cat.Speak. its a classic in getting into this field. But overriding a Method is polymorphism aswell. So yes its a broad concept but from my experience most people refer to the subclasses when they talk about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.