Because a subclass instance may be cast to and treated as an instance of the superclass, overriding methods should uphold the same contracts as the ones in the superclass. Specifically, if the parameters or return type of the superclass method are marked with any of the following, that should not be changed in a subclass: @Nullable, @CheckForNull, @NotNull, @NonNull, and @Nonnull.

Noncompliant Code Example

public class Fruit {

  private Season ripe;
  private String color;

  public void setRipe(@NotNull Season ripe) {
    this.ripe = ripe;
  }

  public @NotNull Integer getProtein() {
    return 12;
  }
}

public class Raspberry extends Fruit {

  public void setRipe(@Nullable Season ripe) {  // Noncompliant
    this.ripe = ripe;
  }

  public @Nullable Integer getProtein() {  // Noncompliant
    return null;
  }
}