Upper Bounds

PHOTO EMBED

Sun Dec 25 2022 20:21:43 GMT+0000 (Coordinated Universal Time)

Saved by @prettyleka #java #generics

public class Box <T extends Number> {
  private T data; 
}
 
Box<Integer> intBox = new Box<>(2);  // Valid type argument
Box<Double> doubleBox = new Box<>(2.5);  // Valid type argument
Box<String> stringBox = new Box<>("hello");  // Error

public static <T extends Number> boolean isZero(T data) {
  return data.equals(0);
}

//multiple bounds
public class Box <T extends Number & Comparable<T>> {
  private T data; 
}
content_copyCOPY