effective-java-3e-source-code icon indicating copy to clipboard operation
effective-java-3e-source-code copied to clipboard

self() in parent is sufficient

Open marcusadrian opened this issue 4 years ago • 1 comments

marcusadrian avatar Aug 07 '20 20:08 marcusadrian

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:

  1. you will get a compile error when NyPizza pizza = new NyPizza.Builder(SMALL).addToppings(SAUSAGE).build();
  2. 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...

Piecewar avatar Aug 22 '20 06:08 Piecewar