Covariant return types

After today’s lecture of “Effective Java” from Joshua Bloch, I can share some knowledge! Looking at the previous post about Recursive Builder Pattern, You may see interesting pattern – self() method. The idea is, to override self()from an abstract/superclass and change its return type to the current type. Current means, that it is the type of the child class, which overrides the method. Well, that is the simplified definition of covariant return types in JAVA.

Recursive Builder Pattern

How to mimic self keyword in JAVA? First, check out our tutorial about Recursive Builder Pattern to understand why it is even needed.

We all know how handy is the builder pattern. Stop. Maybe not all? Do not worry, builder pattern is well explained in “Design Patterns” from O’Reilly. You will get it in 10 mins. The most important reason why it is good to have a builder is to avoid numerous parameters being passed to the constructor. The builder pattern has many variations, but for the sake of this tutorial, we will use the simplest possible form – static inner class. Take a look: