Builder pattern subclassing problem

Run Settings
LanguageJava
Language Version
Run Command
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(); } }
Editor Settings
Theme
Key bindings
Full width
Lines