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();
}
}