What is parametric polymorphism in Java (with example)?

后端 未结 3 606
温柔的废话
温柔的废话 2021-02-07 07: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 p

3条回答
  •  日久生厌
    2021-02-07 08:03

    "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 animals = new ArrayList();
    

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

提交回复
热议问题