Inheriting constructors in Groovy classes
In Java, class inheritance doesn't support the inheriting of constructors for a number of good reasons (leaving the details of constructing an object to the programmer is generally a good idea). There are times when automatically inheriting the constructors of a class would be really useful and make the code less verbose. One of these cases is when inheriting from a class that extends Exception
, where all the constructors are just calling super
. Groovy has a convenient annotation for doing just that, @InheritConstructors
.
In this recipe, we will explore how to use this annotation.
How to do it...
Let's demonstrate the features that the @InheritConstructors
annotation gives:
Create an
Exception
class: one of the classes that are used to communicate that something is horribly wrong with the code or the data:class BusinessException extends Exception { }
Try to instantiate the class using one of the default constructors of the
Exception
class, for instance...