effective-java-3e-source-code
effective-java-3e-source-code copied to clipboard
self() in parent is sufficient
No description provided.
when an abstract self() method is provided in parent
if you extends Builder in your subclass like this:
public static class Builder extends Pizza.Builder<Calzone.Builder>
(wrong import or others)
other than
public static class Builder extends Pizza.Builder<Builder>
there will be a compile error when you implements self() method in subclass, then you will fix this error.
However, if self() method is not an abstract method in parent and use @SuppressWarnings on it, you will find this problem until you try to create the object:
- you will get a compile error when
NyPizza pizza = new NyPizza.Builder(SMALL).addToppings(SAUSAGE).build();
- or a ClassCastException occurs in runtime when
Calzone calzone = new NyPizza.Builder(SMALL).addToppings(SAUSAGE).build();
but I'm not sure whether this example is right or not...