18

It is my understanding that parametric polymorphism is a technique which allows uniform actions over a variety of data(types). Is my knowledge correct?

Is this example parametric polymorphism? I believe it is since Animal.talk allows talk to be called despite the specific animal type (Cat or Dog).

public interface Animal
{
  public String talk();
}

public class Cat implements Animal
{
  public String talk()
  {
    return "Cat says Meow!";
  }
}

public class Dog implements Animal
{
  public String talk()
  {
    return "Dog says Woof! Woof!";
  }
}

import java.util.*;

public class PolymorphismExample
{
  public static void main(String[] args)
  {
    Collection<Animal> animals = new ArrayList<Animal>();
    animals.add(new Cat());
    animals.add(new Dog());
    for (Animal a : animals)
    {
      System.out.println(a.talk());
    }
  }
}

Regards.

edit: if my example is not specifically exhibiting parametric polymorphism please would you provide one? thank you.

Danny Rancher
  • 1,923
  • 3
  • 24
  • 43
  • Looks correct. Basically, you treat Cat and Dog without regard for their actual type based on the Animal type. – dvallejo Apr 16 '12 at 18:23
  • 3
    As a Haskeller, I wouldn't call that parametric polymorphism. A parametric polymorphic function would be something like `LinkedList reverse(LinkedList)` that works the same for all _parameter_ types `T`. A parametric polymorphic function can't call interface methods. – Daniel Fischer Apr 16 '12 at 18:26

3 Answers3

24

"Parametric Polymorphism" is just another term for "Generics" in Java. The idea is simple: you state what types will be used by a particular class, a clear example of this is present in all the collections of the java.util package.

For learning all the nuances of generics in Java, I highly recommend Angelika Langer's FAQ, it explores every corner of the specification.

In your code, this line is an example of using generics:

Collection<Animal> animals = new ArrayList<Animal>();

A collection is specified to hold any object that is an animal.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
10

Wikipedia:

In programming languages and type theory, parametric polymorphism is a way to make a language more expressive, while still maintaining full static type-safety. Using parametric polymorphism, a function or a data type can be written generically so that it can handle values identically without depending on their type. Such functions and data types are called generic functions and generic datatypes respectively and form the basis of generic programming.

So a great example is the standard Java library collections.

For example, Collections.sort is declared as:

public static <T extends Comparable<? super T>> void sort(List<T> list)

It can take a list of objects of type T that is comparable to other T's and sort the list, without worrying about what type T actually is.

It is different from subtype polymorphism: subtype polymorphism is exemplified by the fact that sort can take any sort of List -- an ArrayList, a LinkedList, etc.

trutheality
  • 23,114
  • 6
  • 54
  • 68
4

Precisely. Parametric polymorphism generally refers to generics/templates.

From wikipedia:

Using parametric polymorphism, a function or a data type can be written generically so that it can handle values identically without depending on their type.

Eran Zimmerman Gonen
  • 4,375
  • 1
  • 19
  • 31
  • 3
    why does this page: http://en.wikipedia.org/wiki/Polymorphism_(computer_science) show the example as subtype polymorphism then? what is the difference between subtype polymorphism and parametric polymorphism? – Danny Rancher Apr 16 '12 at 18:25
  • @DannyRancher What you did, on second thought, does resemble more to subtype polymorphism (polymorphism based on inheritance). Parametric polymorphism is when you write any class or function using generics, so that it works for more than one class that it can receive as a generic parameter (e.g. LinkedList). – Eran Zimmerman Gonen Apr 16 '12 at 18:34
  • @DannyRancher I added some detail to my answer to clarify the difference. – trutheality Apr 16 '12 at 18:37