Jump to content

User:Valharmorgulis/sandbox

From Wikipedia, the free encyclopedia

Bounded vs unbounded polymorphism[edit]

Bounded polymorphism[edit]

In object oriented programming languages, polymorphism where the range of types that can be acted upon is restricted is said to exhibit Bounded polymorphism. Subtyping, which is a type of polymorphism frequently made use of in statically typed object oriented programming languages such as Java and C++, in the form of inheritance, exhibits bounded polymorphism. In subtyping, an object that is a sub-type of T can act on type T and it's sub-types only. The below C++ code gives an example of subtyping and shows the bounded polymorphism it exhibits.

class Duck {
        public:
                void quack()
                {
                        cout << "quack";
                }
};      

class Dog: public Duck{
        public:
                void bark()
                {
                        cout << "Bow Bow";
                }
};

int main(int argc, char **argv)
{       
        Duck *duck = new Dog();
        duck->bark();
        return 0;
}

/** Compiling the above source code produces **/
$ g++ test.cpp 
test.cpp:31:8: error: no member named 'bark' in 'Duck'
        duck->bark();
        ~~~~  ^
1 error generated.

In the above code, though the variable duck declared in function main is assigned an object of it's subclass namely Dog, it cannot execute the method bark because the type of variable duck is class Duck which does not contain the method bark. This check for the type a variable to make sure it can do some task is bounded polymorphism.

Unbounded polymophism[edit]

Modern dynamically typed programming languages typically exhibit unbounded polymorphism[1] where if an object is able to respond to a particular method, then it will respond to that method irrespective of it's type. This is also referred to as duck typing. In the below ruby code, the objects Duck, Dog and Cat implement the quack method and when the talk method gets called by passing objects of the three different classes, the quack method gets called as all of them can respond to it. There is no type checking here as compared to the C++ example provided above under bounded polymorphism.

class Duck
        def quack
                puts "Quack Quack"
        end     
end     

class Dog
        def quack
                puts "Quack Quack"
        end
end


class  Cat
        def quack
                puts "Quack Quack"
        end
end

def talk(animal)
        animal.quack;
end     

talk(Duck.new)
talk(Dog.new)
talk(Cat.new)

Output
---------
Quack Quack
Quack Quack
Quack Quack

Unbounded polymorphism vs subtyping[edit]

The advantages of unbounded polymorphism[1] or duck typing[2][3]is that the code is more concise and readable. There are certain languages like Ruby that implement common methods like length on all types which means the programmer does not have to worry whether an object will respond to it or not. The disadvantage is that the programmer has to be more careful while writing code because if an object does not respond to a method call, there is a runtime error and he/she would have to debug it.

Similarly, for subtyping, there is more protection in the form of static type checking during compile time, but the usage is restricted to the super class and it's sub types. The code tends to become less readable as compared to programming languages that exhibit unbounded polymorphism and the user has to know the nuances of the language to understand exactly what is happening.


See also[edit]

References[edit]

  1. ^ a b [Unbounded polymorphism]. Ruby Forums
  2. ^ [github ducktype vs polymorphism]. Github An explanation of Duck Typing vs Polymorphism
  3. ^ [Mixins and Ducktyping]. SAAS-ArmandoFox and David Patterson