If I have a class that contains an enum
type, can I extend this class and override the enum type or add more constants to this enum?
The purpose is that user will b
You can't extend enum
. enums are essentially a final
class. However, enums can implement, so combined with generics, you get:
interface Color{
}
enum CarColor implements Color{
BLUE,
GREEN;
}
enum RacingCarColor implements Color{
BLUE,
GREEN,
YELLOW;
}
class Vehicle {
protected T color;
protected Vehicle(T color){
this.color = color;
}
public T getColor(){
return color;
}
}
class Car extends Vehicle{
public Car(CarColor color){
super(color);
}
}
class RacingCar extends Vehicle{
public RacingCar(RacingCarColor color){
super(color);
}
}
Voila!
If you want to require that the type be a Color
and be an enum, use this bound:
class Vehicle & Color> { ...
Or a curious equivalent:
class Vehicle> { ...