Generic

PHOTO EMBED

Sun Dec 25 2022 20:14:11 GMT+0000 (Coordinated Universal Time)

Saved by @prettyleka #java

public class Box <T> {
  private T data;
 
  public Box(T data) {
    this.data = data; 
  }
 
  public T getData() {
    return this.data;
  }  
}
 
Box box = new Box<>("My String");  // Raw type box
String s2 = (String) box.getData();  // No incompatible type error
String s1 = box.getData();  // Incompatible type error
content_copyCOPY