Can I extend a class and override enclosed enum type?

前端 未结 3 969
生来不讨喜
生来不讨喜 2021-02-05 12:43

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

3条回答
  •  执念已碎
    2021-02-05 13:18

    Can I extend this class and override the enum type or add more constants to this enum?

    The problem in your situation is called hiding rather than overriding enums, and two above enums aren't related to each other. If you referred overriding as extending an enum type, since an enum is already declared with keyword final, you can't create a sub type of an enum type to add more constants.

    The purpose is that user will be able to call getColor() method without knowing if the dynamic object is from base class or derived class

    You're incorrect in overriding the method getColor. Nevertheless, you've made this method in subclass RacingCar has the same signature as the method in superclass Car. You still forgot about the return type that must be same as the return type in superclass or be a subtype of the return type of the overridden method. We have two different enums that shared one name CarColors. In other words, the full qualified name of the enum in subclass is RacingCar.CarColors and in the superclass is Car.CarColors. So far you have broken the rule of overriding methods, not same return type and not a subtype of the superclass's return type.

    In this matter, we can't create a subtype and you don't want to insert YELLOW to the enum CarColors in super class Car, here is my solution.

    interface IColor{
    
    }
    enum CarColors implements IColor{
        BLUE,
        GREEN;
    }
    enum RacingCarColors implements IColor{
        BLUE,
        GREEN,
        YELLOW;
    }
    class Car {
        protected IColor color;
        public Car(IColor color){
            this.color = color;
        }
        public IColor getColor(){
            return color;
        }
    }
    class RacingCar extends Car{
        public RacingCar(IColor color){
            super(color);
        }
        public IColor getColor(){
            return color;
        }
    }
    
    
    
    public class Test{
        public static void main(String[] args) {
            Car car = new RacingCar(RacingCarColors.YELLOW);
            System.out.println(car.getColor());
    
        }
    }
    

    The approach is still overriding the method getColor, but I used one same return type IColor that interface is super type of two enums in your example. This solution completely matched your expectations, but this might be complicated.

提交回复
热议问题