Polymorphism - Define In Just Two Sentences

OopPolymorphismTerminology

Oop Problem Overview


I've looked at other definitions and explanations and none of them satisfy me. I want to see if anybody can define polymorphism in at most two sentences without using any code or examples. I don't want to hear 'So you have a person/car/can opener...' or how the word is derived (nobody is impressed that you know what poly and morph means). If you have a very good grasp of what polymorphism is and have a good command of English than you should be able to answer this question in a short, albeit dense, definition. If your definition accurately defines polymorphism but is so dense that it requires a couple of read overs, then that's exactly what I am looking for.

Why only two sentences? Because a definition is short and intelligent. An explanation is long and contains examples and code. Look here for explanations (the answer on those pages are not satisfactory for my question):

<https://stackoverflow.com/questions/154577/polymorphism-vs-overriding-vs-overloading>
<https://stackoverflow.com/questions/210460/try-to-describe-polymorphism-as-easy-as-you-can>

Why am I asking this question ? Because I was asked the same question and I found I was unable to come up with a satisfactory definition (by my standards, which are pretty high). I want to see if any of the great minds on this site can do it.

If you really can't make the two sentence requirement (it's a difficult subject to define) then it's fine if you go over. The idea is to have a definition that actually defines what polymorphism is and doesn't explain what it does or how to use it (get the difference?).

Oop Solutions


Solution 1 - Oop

Polymorphism allows the expression of some sort of contract, with potentially many types implementing that contract (whether through class inheritance or not) in different ways, each according to their own purpose. Code using that contract should not(*) have to care about which implementation is involved, only that the contract will be obeyed.

(*) In the ideal case, anyway - obviously quite often the calling code has chosen the appropriate implementation very deliberately!

Solution 2 - Oop

Fruit can be eaten, as a general rule, but different types of fruit is eaten in different ways. An apple, which is a fruit, can be eaten (because it is a fruit). A banana can also be eaten (because it is also a fruit), but in a different manner from an apple. You peel it first.

Well, at least I do, but I'm weird in some manners so what do I know.

This illustrates inheritance (fruit can be eaten), polymorphism (something that eats fruit can eat all types of fruit), and encapsulation (a banana has a skin).

Seriously though, object inheritance, polymorphism, encapsulation, virtual things, abstract things, private things, public things, these are all hard concepts. If someone absolutely wants to have a 2-sentence definition of this then please tag the question as a code-golf variant, because two such sentences will have to be so terse that unless you know what it is already you won't learn enough about it to know what you need to learn more about.

Solution 3 - Oop

Polymorphism is declaring a uniform interface that isn't type aware, leaving implementation details to concrete types that implement the interface.

Solution 4 - Oop

Wikipedia: Polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface. Pretty straightforward for me.

Solution 5 - Oop

Actually, there are multiple forms of polymorphism and there is quite some controversy over it; you may even see CS professors who cannot define it properly. I am aware of three types:

  • ad-hoc polymorphism (looks like a duck and walks like a duck => is a duck). Can be seen in Haskell and Python for example.

  • generic polymorphism (where a type is an instance of some generic type). Can be seen in C++ for example (vector of int and vector of string both have a member function size).

  • subtype polymorphism (where a type inherits from another type). Can be seen in most OO programming languages (i.e. Triangle is a Shape).

Solution 6 - Oop

I really understand, why you are asking this question. I understand polymorphism, but I was at a job interview and was asked to give short and clear definition of polymorphism. Because I couldn't give clear and short definition I started thinking about it and here is my definition:

The ability of objects of one type to have one and the same interface, but different implementation of this interface.

Solution 7 - Oop

Definition:

Polymorphism is a $10 word for a $1 idea - that when I ask for something to be done, I don't care how it is achieved as long as the end result is appropriate. As long as the service is provided correctly, I don't care about the implementation.

Discussion

While it's commonly used in software development, especially in systems developed following object oriented principles, Polymorphism is fundamentally a real world principle and should be defined in real world terms, not technological ones.

Examples

When I want to make a phone call, I pick up a phone, dial a number and talk to the party at the other end. I don't care about who made the phone, what technology it uses, whether it's wired, wireless, mobile or VOIP, or whether it's under warranty.

When I want to print a document, I print it. I don't care about the implementation language, brand of printer, style of connection, choice of consumable or quality of paper.

Solution 8 - Oop

Multiple implementations of the same interface.

Example: Many models of telephone implement the numeric keypad interface.

Solution 9 - Oop

Polymorphism is a object oriented strategy used when designing object models, to help simplify the code. At it's core polymorphism is the ability to define two simillar yet different objects, and to then treat the two objects as if they are the same.

Ok that's hard....

Solution 10 - Oop

I just thought I'd add my own interpretation of what polymorphism is: Very generically, polymorphism is the act of providing a single interface to entities of different types.

That's rather generic, but that's the only way I can think of to wrap all three types of polymorphisms I know about: ad hoc, parametric and subtype. I'll go in more details below, and have sorted polymorphism types by name, alphabetically. The one you're interested on is most probably subtype polymorphism, which is the last one.

Ad hoc polymorphism

Ad hoc polymorphism is the act of providing multiple implementations of the same method for different parameter types. In OOP, it's generally known as method overloading. For example:

public String format(int a) {
    return String.format("%2d", a);
}

public String format(Date a) {
    return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(a);
}

Both format methods share a single interface, but they work on entities of different types.

Parametric polymorphism

Parametric polymorphism is the act of making a class (or method) work on a type that is itself a parameter of the class (or method). It's often referred to as generics.

For example, Java's List[T] expects a parameter T at instantiation time, and this parameter defines the type of the resulting object.

Note for purists that I'm purposefully ignoring raw types as I feel they'd just muddy the waters in this context.

List[String] and List[Date] share a single interface, but work on (and are) different types.

Subtype polymorphism

Subtype polymorphism is probably what you initially meant in your question: It's the act of providing a single interface to multiple implementations of the same type.

To use the customary example: Animal provides a contract that all implementations must respect. Dog is an Animal, and as such supports all operations that Animal declares. According to the Liskov substitution principle, this allows you to use an instance of Dog where an instance of Animal is expected (but not the other way around).

If Cat and Dog are both subclasses of Animal, then they share a single interface but are in fact different types.

I'm going off in a bit of a tangent here, but subtype polymorphism is (I think) the only one that allows overriding: the act of redefining the behaviour of a method defined by a parent class. This is often confused with overloading which, as we saw before, is a type of polymorphism and doesn't in fact need subclassing (nor does it need classes, really).

Solution 11 - Oop

polymorphism == multiple classes + same method signatures + class-specific behavior.

Solution 12 - Oop

It seems that the best definitions are provided here, so let me add my two cents please, just for other observers. I hope it could help more.

There are two kinds of polymorphism:

1. Compile-time (static) polymorphism or (ad hoc) polymorphism.

That is simply method overloading and operator overloading

2.  Run time or (dynamic) polymorphism.

The first term is inherited from the Java and C++ terminology.

But in the .NET terminology only the second one (I mean run time polymorphism) is really supposed as polymorphism and simply called polymorphism.

And as far as I know there are three methods for implementing (run time) polymorphism.

 1. Parametric polymorphism or simply the use of generics (templates in C++).

 2. Inheritance-based polymorphism or subtyping.

 3. Interface-based polymorphism.

A simple example Of interface-based polymorphism:

interface Imobile
{
    void Move();
}

class Person :Imobile
{
    public void Move() { Console.WriteLine("I am a person and am moving in my way."); }
}

class Bird :Imobile
{
    public void Move() { Console.WriteLine("I am a bird and am moving in my way."); }
}

class Car :Imobile
{
    public void Move() { Console.WriteLine("I am a car and am moving in my way."); }
}


class Program
{

    static void Main(string[] args)
    {
        // Preparing a list of objects
        List<Imobile> mobileList = new List<Imobile>();

        mobileList.Add(new Person());
        mobileList.Add(new Bird());
        mobileList.Add(new Car());

        foreach (Imobile mobile in mobileList)
        {
            mobile.Move();
        }

        // Keep the console open
        Console.WriteLine("Press any key to exit the program:");
        Console.ReadKey();
    }
}

Output:

 I am a person and am moving in my way.
 I am a bird and am moving in my way.
 I am a car and am moving in my way.
 Press any key to exit the program:

Solution 13 - Oop

Polymorphism is a software coding abstraction where several different underlying entities (usually data, but nit always) all share a common interface which allows them to look and act identical at runtime. We use this as a development technique to enforce consistent behavior over a wide range of similar, but not identical instances with an absolute minimal implementation, thus reducing the expectation for bugs and inconsistencies.

Paul.

Solution 14 - Oop

Multiple forms of a single object is called Polymorphism.

Solution 15 - Oop

Polymorphism

Different objects can respond to the same message in different ways, enable objects to interact with one another without knowing their exact type.

Via: http://www.agiledata.org/essays/objectOrientation101.html

Solution 16 - Oop

Polymorphism is ability of an object to appear and behave differently for the same invocation. ex: each animal appear and sound differently ( when you hit it :) )

Solution 17 - Oop

Polymorphism is a feature of programming languages that allows an object to be treated as an instance of its supertype.

Solution 18 - Oop

Giving a single name to a set of analogous operations on different types. When done well, the analogy is obvious e.g. "adding" numbers arithmetically and "adding" strings by concatenation (which sums their lengths).

Solution 19 - Oop

This is the definition that I've always followed:

Two objects are polymorphic (with respect to a particular protocol) between them, if both respond to the same messages with the same semantic.

Polymorphism is about messages, is about being able to respond the same set of messages with the same semantic.

If two object CAN respond to empty? but the semantic of the message is different, then.. they are not polymorphic.

Solution 20 - Oop

Polymorphism at the lower level is the ability to invoke methods that are defined by the implementors of an interface from the interface instance.

Solution 21 - Oop

Polymorphism is a programming feature that allows an object to have many types ('shapes') and lets you treat it as any of those types depending on what you need to do without knowing or caring about its other types.

Solution 22 - Oop

Polymorphism is language functionality allowing high-level algorithmic code to operate unchanged on multiple types of data. And the other sentence, whatever it was for... ;-P.

( The types C++ supports are listed and contrasted in my answer: https://stackoverflow.com/questions/5854581/polymorphism-in-c/5854862#5854862 )

Solution 23 - Oop

Entities of the same type (that is, implemented same interface or derived from same class), behave in different ways (under same method name).

Solution 24 - Oop

Polymorphism concept became a phenomenon lately. Here is the actual drift. Runtime defines which submethod should be invoked by a reference of a super class. Now, what does mean in practice? It means actually nothing. You can code simply without polymorphism. So, why? Because, if we haven't got the polymorphism, we had to memorize all the subclass functions definitions. Polymorphism saves us from this in practice.

You can define a list as follows:

List list = new List();

but if you check for IList, you can benefit of the interface as:

IList list = new List();

and use the IList reference freely. Assuming IList is also implemented in another class, you can use methods of that unknown class via again IList reference without trying to remember that class name. Marvelous, isn't it?

Now, more valuable information is coming:
Java is by default polymorphic, whereas .NET and C++ are not, in MS, you have to declare the base function virtual (and in .NET override keyword).

Also, there are 2 integral rules in polymorphism. One is inheritance (via interface impl. or via class extending) and the other is overriding. Without overriding, polymorphism doesn't exist. Note that method overloading (which always in a single class) is also a type of "minimalistic" polymorphism.

Solution 25 - Oop

I think implementation of methods of the same signature in different classes (having some sort of inheritance relation either using extends or implements) is method overriding and also polymorphism because in this way we are achieving many forms of the same method signature.

Solution 26 - Oop

For a given method signature, different method implementations are run for different, hierarchically related, classes.

Solution 27 - Oop

Polymorphism is the ability of using different classes that implement a common interface (or extend a common base class) in a common way, without needing to now the specific implementation, and using only the methods available in the common interface.

Ie: In Java, as ArrayList and LinkedList both implement List, if you declare a variable as List, you can always perform the operations allowed in List, no matter which if you variable was instanced as an ArrayList or a LinkedList.

Solution 28 - Oop

I guess sometimes objects are dynamically called. You are not sure whether the object would be a triangle, square etc in a classic shape poly. example.

So, to leave all such things behind, we just call the function of derived class and assume the one of the dynamic class will be called.

You wouldn't care if its a sqaure, triangle or rectangle. You just care about the area. Hence the getArea method will be called depending upon the dynamic object passed.

Solution 29 - Oop

Polymorphism is the ability of a function to automatically adapt to accept input data of different data types. You can 'Add' two doubles '1.1' and '2.2' and get '3.3' or 'Add' two strings "Stack" and "Overflow" and get "StackOverflow".

Solution 30 - Oop

Polymorphism is when different objects respond to the same method in a different way. For example, a car moves on the road while a person walks on the road. Those are two objects responding to the same road in a different way.

Solution 31 - Oop

A single class doing different methods is called polymorphism.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark TestaView Question on Stackoverflow
Solution 1 - OopJon SkeetView Answer on Stackoverflow
Solution 2 - OopLasse V. KarlsenView Answer on Stackoverflow
Solution 3 - OopEran GalperinView Answer on Stackoverflow
Solution 4 - OopOtávio DécioView Answer on Stackoverflow
Solution 5 - OopAfdView Answer on Stackoverflow
Solution 6 - OopNikolay TraykovView Answer on Stackoverflow
Solution 7 - OopBevanView Answer on Stackoverflow
Solution 8 - OopDoug KnesekView Answer on Stackoverflow
Solution 9 - OopJoshBerkeView Answer on Stackoverflow
Solution 10 - OopNicolas RinaudoView Answer on Stackoverflow
Solution 11 - OopS.LottView Answer on Stackoverflow
Solution 12 - OopsiamakView Answer on Stackoverflow
Solution 13 - OopPaul W HomerView Answer on Stackoverflow
Solution 14 - OopmilotView Answer on Stackoverflow
Solution 15 - OopMattView Answer on Stackoverflow
Solution 16 - OopMallikView Answer on Stackoverflow
Solution 17 - OopTarkaDaalView Answer on Stackoverflow
Solution 18 - Oopjoel.neelyView Answer on Stackoverflow
Solution 19 - OopClaudio AcciaresiView Answer on Stackoverflow
Solution 20 - OopIgor ZevakaView Answer on Stackoverflow
Solution 21 - OopJeff SternalView Answer on Stackoverflow
Solution 22 - OopTony DelroyView Answer on Stackoverflow
Solution 23 - Ooppaul paulView Answer on Stackoverflow
Solution 24 - OopjazziiiloveView Answer on Stackoverflow
Solution 25 - OopanandView Answer on Stackoverflow
Solution 26 - OopPaoloView Answer on Stackoverflow
Solution 27 - OopGaRRaPeTaView Answer on Stackoverflow
Solution 28 - OopKapil DView Answer on Stackoverflow
Solution 29 - OopJ-DizzleView Answer on Stackoverflow
Solution 30 - Oopuser2342589View Answer on Stackoverflow
Solution 31 - OopKINGView Answer on Stackoverflow