Johan Zahri wrote:Does anyone knows a better way of the usage of generic to avoid using
I was thinking well since I know that XResult has a way to instantiate itself there is no way that the casting would not work., would it?
But it just doesn't feel good using casting, somehow.
Well spotted. "Code smell" is a good sense to acquire, so don't ignore it.
And to answer your question: yes there is - make Result generic too.
In this case, it's as simple as:
then whatever class extends Result uses the notation:
which results in a
getNewInstance() method that returns a
Failure.
BTW, generic types, by convention, are usually a single upper-case letter, rather than things like '
XResult'. This is so you can distinguish them from regular class/interface names. The norm is to use '
T' (for 'Type') unless you have a good reason not to, but it's not required. For instance, an interface like
Map<K, V> uses its notation to distinguish between 'Keys' and 'Values'.
HIH
Winston