Java Reference
In-Depth Information
public Shape getShape () {
return shape ;
}
private RegularPolygon ( int sides ) {
switch ( sides ) {
case 3 :
// We assume that we have some general constructors
// for shapes that take the side length and
// angles in degrees as parameters
shape = new Triangle ( 1 , 1 , 1 , 60 , 60 , 60 );
break ;
case 4 :
shape = new Rectangle ( 1 , 1 );
break ;
case 5 :
shape = new Pentagon ( 1 , 1 , 1 , 1 , 1 , 108 , 108 , 108 , 108 , 108 );
break ;
case 6 :
shape = new Hexagon ( 1 , 1 , 1 , 1 , 1 , 1 , 120 , 120 , 120 , 120 , 120 , 120 );
break ;
}
}
}
These parameters (only one of them in this example) are passed to the constructor
to create the individual enum instances. As the enum instances are created by the
Java runtime, and can't be instantiated from outside, the constructor is declared as
private.
Enums have some special properties:
• All (implicitly) extend java.lang.Enum
• May not be generic
• May implement interfaces
• Cannot be extended
• May only have abstract methods if all enum values provide an implementation
body
• May only have a private (or default access) constructor
Annotations
Annotations are a specialized kind of interface that, as the name suggests, annotate
some part of a Java program.
For example, consider the @Override annotation. You may have seen it on some
methods in some of the earlier examples, and may have asked the following ques‐
tion: what does it do?
Search WWH ::




Custom Search