Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is parametric polymorphism in Java (with example)?

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.

like image 903
Danny Rancher Avatar asked Apr 16 '12 18:04

Danny Rancher


People also ask

What is parametric polymorphism example?

Techopedia Explains Parametric Polymorphism For example, if a programming function operates on two different values, the values may be attached, even though they do not have the same data types. An example is joining a list of integers with a floating point value.

What is parametric polymorphism in Java?

Parametric polymorphism stipulates that within a class declaration, a field name can associate with different types and a method name can associate with different parameter and return types. The field and method can then take on different types in each class instance (object).

What is the polymorphism in Java with example?

In Java, polymorphism refers to the ability of a class to provide different implementations of a method, depending on the type of object that is passed to the method. To put it simply, polymorphism in Java allows us to perform the same action in many different ways.

What is polymorphism explain it with examples?

The word “polymorphism” means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. A real-life example of polymorphism is a person who at the same time can have different characteristics.


1 Answers

"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.

like image 190
Óscar López Avatar answered Oct 07 '22 00:10

Óscar López