Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Groovy enables one class to extend another, just as interfaces can, though classes extend at most one class. We can test for extended classes with the 'in' operator, just like with implemented interfaces:

Public instance fields, properties, and methods defined on an extended class are also available on the extending class:

We can use the 'private' and 'protected' modifiers to restrict the visibility of instance methods, properties, and fields:

Public static fields, properties, and methods are inherited by extending classes:

We can make static methods, properties, and fields private or protected:

We can define what's called an "abstract class", a class with only some methods defined, the others being only declarations just like in interfaces. An abstract class and each method declaration in it must be modified with the keyword 'abstract':

Whether a method is static or not is part of its definition, not its declaration. So interface and abstract methods may not be declared static.

At the other end from abstract classes and methods are "final classes" and "final methods". A final class may not be extended; a final method may not be overriden:


Just as a class's constructor can call another constructor at the beginning of its code, so also it can call a constructor on the superclass at the beginning of its code:

Using Classes by Extending Them

Some classes supplied with Groovy are intended to be extended to be used. For example, FilterInputStream, FilterOutputStream, FilterReader, and FilterWriter:

We can similarly extend FilterInputStream, FilterReader, and FilterWriter.

The Object Hierarchy

All classes are arranged in a hierarchy with java.lang.Object as the root. Here are those we've met so far; those labelled as such are abstract and final classes:

  • No labels