Junior — Middle
Can you explain what is meant by parametric polymorphism in programming?
sobes.tech AI
Answer from AI
Parametric polymorphism is the ability to write generic code that works with different data types without specifying the specific types in advance. In Java, this is implemented through generics.
For example, you can create a class or method that works with any data type, and the type will be specified when used:
public class Box<T> {
private T value;
public void set(T value) { this.value = value; }
public T get() { return value; }
}
Box<String> stringBox = new Box<>();
stringBox.set("Example");
String s = stringBox.get();
Here, T is a type parameter that allows using a single class for different data types without code duplication.