class Base {
public Base withBaseParam(String param) {
System.out.println("Here in Base class with " + param);
return this;
}
public Base build() {
System.out.println("Building in Base class");
return this;
}
}
class Subclass extends Base {
public Subclass withSubclassParam(String param) {
System.out.println("Here in Subclass with " + param);
return this;
}
}
class AnotherSubclass extends Base {
public AnotherSubclass withAnotherSubclassParam(String param) {
System.out.println("Here in AnotherSubclass with " + param);
return this;
}
}
public class Client {
public static void main(String[] args) {
new Subclass()
.withSubclassParam("foo")
.build(); // OK
new Subclass()
.withBaseParam("foo")
.withSubclassParam("foo") // COMPILE ERROR - our instance is now of class Base
.build();
}
}