Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain Polymorphism to me? [duplicate]

Possible Duplicate:
Try to describe polymorphism as easy as you can

I've never been able to fully comprehend what polymorphism is. Can someone explain, perhaps with use of an example, what it is and how it works? Just the basics.

like image 270
muttley91 Avatar asked Oct 12 '11 23:10

muttley91


3 Answers

Perhaps it's easiest to start with a non-computer analogy.

Consider if you told somebody to "Go to the store and buy some of your favorite food for supper."

If you said this to a 14 year-old son, he'd probably ride his bike to the store, have to pay cash for the food, and you'd be having pizza for supper.

If you said it to your wife, she'd probably drive to the store, use a card to pay for the food, and you might be eating chicken Cordon Bleu with Chardonnay instead.

In a program, things work out a bit the same way: you specify something at a relatively abstract level (go to the store and get supper). Each object provides its own concrete implementation of how to implement that, and in many cases provides for some variation in exactly what it's going to do (e.g., like the differences in favorite foods above).

Of course, when you're programming, most of that requires a specification that's a lot more detailed and unambiguous. The general idea remains the same though. For the scenario above, you might have a person base class (or interface) that defined methods like go to store and select favorite food and pay for purchase. You'd then have implementations of that like adult and teenager, each of which defined its own method of going to the store, selecting favorite food, and paying for a purchase. Those methods would be polymorphic, because each implementation would have its own way of carrying out the higher-level command you gave.

like image 102
Jerry Coffin Avatar answered Oct 16 '22 11:10

Jerry Coffin


Literally, polymorphism means "having multiple forms". In programming, if a variable can hold more than one type of value, then that's a kind of polymorphism. If functions can process more than one type of parameter, that's also polymorphism.

Object oriented languages have polymorphism through the class hierarchy: a reference to a base class or interface can refer to multiple types of object, as long as those other types are derived from the base. This is called subtype polymorphism.

Generic programming is another kind of polymorphism. By applying parameters to types, the same bit of code can handle multiple types of object. This is called parametric polymorphism.

Operator overloading, and overloading of methods within a class are another kind of polymorphism, known as ad hoc polymorphism, because it's less systematic than parametric or subtype polymorphism.

like image 16
Bruce Attah Avatar answered Oct 16 '22 12:10

Bruce Attah


Polymorphism means the ability to choose the exact called function at runtime depending on the current context.

This can be done by describing an interface class from where others will derive. One can use in his code only the interface instead of using certain classes. This gives the programmer the ability to choose the best implementation for his problems.

As an example one can use arrays. There might be two possible two implementations, one when a array is sparse (lots of zeros) and one when the array is full. Instead of using one class direct one would define the interface of the array class and then in the context choose the best implementation. See the following code as an example (C++ style) of an integer array:

 class arrayInterface{
    ...
    virtual int getElement(elementPosition)=0
    ...
}

class sparseArray : public arrayInterface{
    ...
    virtual int getElement(elementPosition){
        implementation
    }
    ...
}

class fullArray : public arrayInterface{
    ...
    virtual int getElement(elementPosition){
        implementation
    }
    ...
}

main(){
    arrayInterface* array =  new fullArray();
    // this uses now the implementation specified by fullArray
    int element = array->getElement(10)

    delete array;
    array = new sparseArray
    // this uses now the implementation specified by sparseArray
    int element = array->getElement(10)

}
like image 2
tune2fs Avatar answered Oct 16 '22 11:10

tune2fs