March 2019
Intermediate to advanced
242 pages
6h 21m
English
The following is another example of an enum, that works with a set of subclasses by passing instances of subclasses to the enum constructor. To get the point across, I've modified the Size enum, which we have been working on with since the beginning of this chapter. The following is the modified code:
class Measurement {} // base class
class Small extends Measurement { // derived class
String text = "Small"; // state specific to class //Small
}
class Medium extends Measurement { // derived class
public int getLength() { // behavior specific to class //Medium
return 9999;
}
}
class Large extends Measurement {} // derived class
enum Size {
SMALL(new Small()), // constant created using Small //instance ...Read now
Unlock full access