4

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?

Community
  • 1
  • 1
Sean Hill
  • 1,297
  • 1
  • 13
  • 21
  • The 'Programmers' StackExchange site (http://programmers.stackexchange.com) may be a better place for you to as this question. – narner Apr 24 '15 at 16:53
  • 1
    @narner: the question is fine here. It is still a programming question. – IAbstract Apr 24 '15 at 17:19
  • @narner - Even the Computer Science Stack Exchange would be a good place for this. – Mr. Llama Apr 24 '15 at 17:19
  • I've tried to suggest some mechanism that cross-references certain questions between highly relevant sites but, iirc was shot down. :) – IAbstract Apr 24 '15 at 17:20
  • 2
    I'm voting to close this question as off-topic because it has been [cross-posted to Programmers](http://programmers.stackexchange.com/questions/280085/what-is-polymorphism-explained-simply). – BoltClock Apr 24 '15 at 17:46
  • @narner when mentioning other sites, it is typically helpful to point that [cross-posting is frowned upon](http://meta.stackexchange.com/tags/cross-posting/info) – gnat Apr 24 '15 at 20:29

3 Answers3

4

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.

Lydia Ralph
  • 1,455
  • 1
  • 17
  • 33
2

Generally, the ability to appear in many forms. In object-oriented programming, polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes. For example, given a base class shape, polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the area method to it will return the correct results. Polymorphism is considered to be a requirement of any true object-oriented programming language (OOPL).

Simply method overloading and method overriding...

Sanke
  • 676
  • 2
  • 13
  • 30
  • So if I have a Shape that has attribute type = triangle, and another Shape that has attribute type = square, and I call the .area() method, the function returns different equations depending on the type. Is this polymorphism? – Sean Hill Apr 24 '15 at 16:58
  • 1
    In essence, yes. If you process a collection of Shape that are a mixture of different types of Shape, calling area() on each would be done polymorphically. – pens-fan-69 Apr 24 '15 at 17:00
  • This *"... ability to redefine methods ..."* is incorrect. Redefining a method is much more broad than the intention of polymorphic implementation. The correct phrase would be *"... ability to override methods ..."*. – IAbstract Apr 24 '15 at 17:11
  • It should be noted that **method overloading** is language specific. C# will allow method overrides within a derived class but you cannot overload the base method from the derived class. However, in a language like Python that does not enforce type-safety, you can actually overload a base method from the derived class. – IAbstract Apr 24 '15 at 17:15
0

A minimal but comprehensible Java example:

public class Animal {
    public void eat() {
        System.out.println("wow!");
    }
}

public class Horse extends Animal {
    public void eat() {
        System.out.println("iiiihhh!");
    }
}

public class Dog extends Animal {
    public void eat() {
        System.out.println("whoa!");
    }
}

By overriding their parent classes method, both Dog and Horse classes can modify their eat() method, while you can use them more generically by calling casting to the superclass Animal.

Animal animal = new Animal();
Animal dog = new Dog();
Animal horse = new Horse();

animal.eat();  // prints "wow!"
dog.eat();     // prints "whoa!"
horse.eat();   // prints "iiiihhh!"

As said by Sanke, it is simply method overloading and method overriding.

Bruno Toffolo
  • 1,504
  • 19
  • 24