What is polymorphism, explained simply?

Sean Hill

I know this is a duplicate question and a little "soft" but I have not liked any of the other explanations out there and would like to hear a simple response that does not generalize so much as to be confusing.

For example What is polymorphism, what is it for, and how is it used?

Is polymorphism just "being able to perform functions (or is it methods?) of an interface, such as adding, subtracting, etc, on objects of different data types such as integers, floats, etc"?

Is it basically operator overloading? Or templating?

Lydia Ralph

Polymorphism is just a fancy word which means you can use a more general term to refer to a specific type of object.

It goes hand in hand with interfaces

Interface: Same word, several flavours

Instead of saying "I got a new Vauxhall Corsa", you could simply say "I got a new car". This statement would also be true if you'd just got a Ford Fiesta, as that is also a car. The flexibility (polymorphism) of the English word 'car' means that you don't have to specify exactly which kind of car it is. Your audience will know that you have a modern contraption on your front drive which is designed to beep, steer, and drive down the road, even though the exact mechanisms of Vauxhall and Ford engines may be different from each other.

Polymorphism takes this interface and lets you refer to your Ford Fiesta as simply a Car:

Car car = new Ford();

From this blog:

Polymorphism means using a superclass variable to refer to a subclass object. For example, consider this simple inheritance hierarchy and code:

abstract class Animal {
    abstract void talk();
}
class Dog extends Animal {
    void talk() {
        System.out.println("Woof!");
    }
}
class Cat extends Animal {
    void talk() {
        System.out.println("Meow.");
    }
}

Polymorphism allows you to hold a reference to a Dog object in a variable of type Animal, as in:

Animal animal = new Dog();

PS Given the other answers, you may also want to know the difference between an abstract class and an interface.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What is polymorphism in Javascript?

From Dev

What is Levity polymorphism

From Dev

What is the need Polymorphism in OOP?

From Dev

What is the need Polymorphism in OOP?

From Java

What is polymorphism, what is it for, and how is it used?

From Dev

what is the difference between polymorphism and inheritance

From Dev

What is the difference between polymorphism and overloading?

From Dev

Polymorphism - What am I not getting?

From Dev

what is the difference between polymorphism and inheritance

From Java

What are file descriptors, explained in simple terms?

From Dev

What is this syntax called? And where is it explained in the Scala documentation?

From Dev

What is the motivation behind static polymorphism in C++?

From Dev

What is F-Bounded Polymorphism in TypeScript

From Java

What is the difference between dynamic and static polymorphism in Java?

From Dev

what is the equivalent of pure abstract function in static polymorphism?

From Dev

What are the problems that can arise in an implementation that lacks polymorphism?

From Dev

Polymorphism:what is the real type of the called method?

From Dev

What happened to polymorphism for jOOQ-generated classes?

From Dev

What is the motivation behind static polymorphism in C++?

From Dev

What are the problems that can arise in an implementation that lacks polymorphism?

From Dev

Purescript row polymorphism. What is the correct syntax?

From Dev

What is right to do to maintain the principle of inheritance polymorphism?

From Dev

what is difference between "variance explained " in Random Forest and "merror" in XGBoost

From Dev

What data is saved in Hyperledger Fabric for the situation as explained below

From Java

What is the shortest way to simply sort an array of structs by (arbitrary) field names?

From Dev

What is the difference between a socket that simply emits and one that volatile emits?

From Dev

What are the very specific advantages to WebJars compared to simply using Bower?

From Dev

What is the simplest and fastest way of storing and querying simply-structured data?

From Dev

What is Replace Conditional with Polymorphism Refactoring? How is it implemented in Ruby?

Related Related

  1. 1

    What is polymorphism in Javascript?

  2. 2

    What is Levity polymorphism

  3. 3

    What is the need Polymorphism in OOP?

  4. 4

    What is the need Polymorphism in OOP?

  5. 5

    What is polymorphism, what is it for, and how is it used?

  6. 6

    what is the difference between polymorphism and inheritance

  7. 7

    What is the difference between polymorphism and overloading?

  8. 8

    Polymorphism - What am I not getting?

  9. 9

    what is the difference between polymorphism and inheritance

  10. 10

    What are file descriptors, explained in simple terms?

  11. 11

    What is this syntax called? And where is it explained in the Scala documentation?

  12. 12

    What is the motivation behind static polymorphism in C++?

  13. 13

    What is F-Bounded Polymorphism in TypeScript

  14. 14

    What is the difference between dynamic and static polymorphism in Java?

  15. 15

    what is the equivalent of pure abstract function in static polymorphism?

  16. 16

    What are the problems that can arise in an implementation that lacks polymorphism?

  17. 17

    Polymorphism:what is the real type of the called method?

  18. 18

    What happened to polymorphism for jOOQ-generated classes?

  19. 19

    What is the motivation behind static polymorphism in C++?

  20. 20

    What are the problems that can arise in an implementation that lacks polymorphism?

  21. 21

    Purescript row polymorphism. What is the correct syntax?

  22. 22

    What is right to do to maintain the principle of inheritance polymorphism?

  23. 23

    what is difference between "variance explained " in Random Forest and "merror" in XGBoost

  24. 24

    What data is saved in Hyperledger Fabric for the situation as explained below

  25. 25

    What is the shortest way to simply sort an array of structs by (arbitrary) field names?

  26. 26

    What is the difference between a socket that simply emits and one that volatile emits?

  27. 27

    What are the very specific advantages to WebJars compared to simply using Bower?

  28. 28

    What is the simplest and fastest way of storing and querying simply-structured data?

  29. 29

    What is Replace Conditional with Polymorphism Refactoring? How is it implemented in Ruby?

HotTag

Archive