Skip to content

Instantly share code, notes, and snippets.

@odrotbohm
Created December 3, 2018 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odrotbohm/245298e5294f226343ad32778c468fd0 to your computer and use it in GitHub Desktop.
Save odrotbohm/245298e5294f226343ad32778c468fd0 to your computer and use it in GitHub Desktop.
class Sample {
public this someMethod() {
// …
return this;
}
}
class Subtype extends Sample {};
Subtype instance = new Subtype();
Subtype result = instance.someMethod(); // Does obviously not compile
// Alternatives
// - Override someMethod() in Subtype and cast to Subtype
// - Use self referential generics Sample<T extends Sample<T>> and T someMethod() { return (T) this; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment