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.
LinkedList<T> reverse(LinkedList<T>)
that works the same for all parameter typesT
. A parametric polymorphic function can't call interface methods. – Daniel Fischer