Skip to content

Instantly share code, notes, and snippets.

@JakeWharton
Created January 7, 2016 05:59
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JakeWharton/14547311b1f145cb0695 to your computer and use it in GitHub Desktop.
Save JakeWharton/14547311b1f145cb0695 to your computer and use it in GitHub Desktop.
Covariant return types generate an extra method in bytecode. This compounds in each subclass further specializing the type.
interface Thing {
Object thing();
}
class CharSequenceThing implements Thing {
@Override public CharSequence thing() {
return "CharSequence!";
}
}
class StringThing extends CharSequenceThing {
@Override public String thing() {
return "String!";
}
}
$ javap -p Thing CharSequenceThing StringThing
Compiled from "CovariantReturnTypes.java"
interface Thing {
public abstract java.lang.Object thing();
}
Compiled from "CovariantReturnTypes.java"
class CharSequenceThing implements Thing {
CharSequenceThing();
public java.lang.CharSequence thing();
public java.lang.Object thing();
}
Compiled from "CovariantReturnTypes.java"
class StringThing extends CharSequenceThing {
StringThing();
public java.lang.String thing();
public java.lang.CharSequence thing();
public java.lang.Object thing();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment