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:

  1. Create an Exception class: one of ...

Get Groovy 2 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.