# Unbounded polymorphism

**URL:** <https://www.ruby-forum.com/t/unbounded-polymorphism/84471>\
**Category:** Ruby\
**Created:** [February 5, 2007, 1:50am UTC](https://www.ruby-forum.com/t/unbounded-polymorphism/84471 "2007-02-05T01:50:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 5, 2007, 1:50am UTC](https://www.ruby-forum.com/t/unbounded-polymorphism/84471/1 "2007-02-05T01:50:34Z")

</div>

Hi,  
One of the features, I have heard that Ruby has, is unbounded  
polymorphism.  
How exactly is it different from polymorphism in languages like Java  
and C#?  
Thanks

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 5, 2007, 3:00am UTC](https://www.ruby-forum.com/t/unbounded-polymorphism/84471/2 "2007-02-05T03:00:40Z")

</div>

On Feb 4, 7:48 pm, removed\_email\_address@domain.invalid wrote:

> Hi,  
> One of the features, I have heard that Ruby has, is unbounded  
> polymorphism.  
> How exactly is it different from polymorphism in languages like Java  
> and C#?

Think of a bound as an upper limit with respect to the class  
hierarchy. If a method lists a parameter as taking a Shape, and  
Circle and Rectangle are subclasses of Shape, then instances of Shape,  
Circle, and Rectangle can be passed into that method (unless Shape is  
an abstract class in which case there are no instances of it).  
Instances that are not at or below Shape are excluded from being  
passed in as that parameter.

The same thing is true with Java generics. If you declare a variable  
as being of type List, then only instances of Shape, Circle,  
and Rectangle can be added to that list.

All this is possible due to the static type of Java.

Ruby, on the other hand, does not have static typing. Types are not  
associated with variable or parameter declarations. So you can assign  
any piece of data to any variable and you can pass in any type of data  
into any parameter.

Now that may sound a little wild initially. However, if you try to  
call an unsupported method on that variable or parameter, then you’ll  
get a runtime error and an opportunity to debug.\* But if that data  
supports all the methods that are called on it, it doesn’t matter what  
type it is. You often hear this referred to as “Duck Typing”, because  
if it walks like a duck and talks like a duck then for all intents and  
purposes, it’s a duck (i.e., if it can _do_ what you ask it to do,  
then you don’t care what it “really” is).

Hope that helps,

Eric

\*: There’s a caveat to this in that you can create a hook to “capture”  
these calls to missing methods and respond to them programatically.  
This is done by declaring a method named “method\_missing” that will,  
as you might guess, get called when the method is missing.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 5, 2007, 3:58am UTC](https://www.ruby-forum.com/t/unbounded-polymorphism/84471/3 "2007-02-05T03:58:43Z")

</div>

That really helped me understand it clearly.  
Thanks 🙂
