Builder pattern SELF referencing

Run Settings
LanguageJava
Language Version
Run Command
class Base implements VariantA<Base>, VariantB<Base> { public Base build() { System.out.println("Building in Base class"); return this; } } interface VariantA<SELF extends VariantA<SELF>> { default SELF withVariantAParam(String param) { System.out.println("Here in Variant A class with " + param); return (SELF) this; } } interface VariantB<SELF extends VariantB<SELF>> { default SELF withVariantBParam(String param) { System.out.println("Here in Variant B class with " + param); return (SELF) this; } } public class Client { public static void main(String[] args) { new Base() .withVariantAParam("foo") .withVariantBParam("bar") .build(); } }
Editor Settings
Theme
Key bindings
Full width
Lines