Forums Register Login

confusion in polymorphism

+Pie Number of slices to send: Send
what is meant by dynamic binding and run time polymorphism and the difference between the two
how many types of polymorphisms are there?
+Pie Number of slices to send: Send
Hello,

You are mixing up some terms. Dynamic binding is not a type of polymorphism; rather dynamic binding is how polymorphism is "implemented" or how it works. And dynamic binding and runtime binding are the same thing. "Dynamic Binding" is the more academic or computer science based name for it. And "run time binding" is a more every day term for it. And as if that wasn't enough, it can also be called "late binding". Let's talk about these in a little more detail for you to help clear things up.

Ploymorphism -- which literally means many shapes -- is the ability of objects to belong to different types. For example, lets say I have a Dog class that implements both an Animal and a Pet interface. The Dog class is polymorphic. It can be an Animal, a Dog, or a Pet.

To take this a bit further, let's say the Animal interface has a method getVocalSound(). As such our Dog class must implement this interface. In its implementation of getVocalSound() it barks. Now lets say I have 15 other classes that implement Animal. They too must all implement the getVocalSound(), but when called they do slightly different things. They all make a vocal sound, but slightly different ones. The Cat class will meow, the Bird class will chirp, the Sheep class will baa, etc. Let's also say the Animal intergace has a getName() method which returns the name of the Animal.

Now to go to the next step... Let's say I write the following method:


{As a side note, in case you are not familiar, Old McDonald is a children song.}

When I wrote this method, I have no idea what type of Animal will be passed in. So I don't know exactly what will happen when the getVocalSound() method is called on the animal parameter passed in. I don't know if the animal is a Dog, a Cat, a Tiger, or some Animal that someone hasn't even coded yet. In fact, not only do I not know, but the code itself doesn't know. The compiler doesn't know. It isn't know until the code is actually run. In fact, I could compile this code today and in 20 years someone could write a class that implements the Animal interface and use my binary code that I complied 20 years earlier. So the exact type of (animal) object is not known in advance, so its exact behavior is implemented at run time. This is dynamic binding, or late binding (or run time binding).

The opposite of dynamic binding is "static binding", also known as "early binding". Early binding is the assignment of variables or expressions at compile time. For example, in the code:


"counter" is static bound or early bound. It is known at compilation time that counter is an int and will always be an int (at least in Java).


You can get some basic definitions of these terms at the PC Magazine IT Encyclopedia:
  • binding time
  • early binding
  • dynamic binding
  • polymorphism

  • You can also read the Wikipedia articles on Polymorphism and dynamic binding. Interestingly, and unbeknownst to me when I was typing this, the Wikipedia article on polymorphism uses a similar Animal example. It's a common example when talking about polymorphism.

    And to answer your last question, there is only one type of polymorphism (as far as I know).

    I hope that helps.
    [ July 25, 2008: Message edited by: Mark Vedder ]
    +Pie Number of slices to send: Send
    thakx a lot for your valuable guidance
    No more fooling around. Read this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 1821 times.
    Similar Threads
    polymorphism
    polymorphism
    Method Overloading and overriding
    4 key points about Oops
    OOP-4 hints please.
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Apr 15, 2024 23:11:11.