March 2019
Intermediate to advanced
242 pages
6h 21m
English
Let's rewrite the enum code from the preceding example, adding a variable type to the enum Size. The bounded type parameter (<T extends Measurement>) restricts the types that can be passed as arguments to the Size enum, to the Measurement class and its derived classes.
The modified code is as follows:
enum Size <T extends Measurement> { // enum with type parameter
SMALL(new Small()),
MEDIUM(new Medium()),
LARGE(new Large());
private T mObj;
Size(T obj) {
mObj = obj;
}
T getMeasurement() {
return mObj;
}
} class Measurement {} class Small extends Measurement ...Read now
Unlock full access