#Enum and UML

1 messages · Page 1 of 1 (latest)

pure basin
#

If I have a Vehicle class that has 3 subclasses, but only 2 of the subclasses use the enum public enum EngineTypes {PETROL, DIESEL, ELECTRIC, UNKNOWN} where should i declare my Enum? In Vehicle or in each of the 2 subclasses, so I am declaring it 2 times.

If say Vehicle declare that enum, how should i draw it in the UML diagram, the 2 subclasses would have a composition relationship with the enum as they uses them as attribute, but what relationship does Vehicle has with the enum? Since it only handles the declaration.

lethal frigateBOT
#

<@&987246841693360200> please have a look, thanks.

pure basin
#

or should i be using interface

drifting ore
#

okay there are a few answers here

#

i am going to deliberately choose to ignore the UML part of your question

#

because UML is confusing as heck

#

but okay Java side -

#

yes, you should be using an interface for Vehicle

#

but we can do the entire thought process with an abstract class too

#
abstract class Vehicle {}

enum EngineType {PETROL, DIESEL, ELECTRIC, UNKNOWN}

class Car extends Vehicle {
    EngineType engineType;
}

class Boat extends Vehicle {
    EngineType engineType;
}

class Bike extends Vehicle {

}
#

if only two subtypes have an engine you have three options

#
  1. Make a method/field on Vehicle but just say "it might be null if the vehicle does not have an engine"
#
abstract class Vehicle {
    EngineType engineType = null;
}

enum EngineType {PETROL, DIESEL, ELECTRIC, UNKNOWN}

class Car extends Vehicle {
    // Set an engine type
}

class Boat extends Vehicle {
    // Set an engine type
}

class Bike extends Vehicle {
    // Leave engine type as null
}
#
  1. Make an "intermediate" subclass which has the engine
#
abstract class Vehicle {
    EngineType engineType = null;
}

enum EngineType {PETROL, DIESEL, ELECTRIC, UNKNOWN}

class VehicleWithEngine extends Vehicle {
    EngineType engineType;
}

class Car extends VehicleWithEngine {
    // Set an engine type
}

class Boat extends VehicleWithEngine {
    // Set an engine type
}

class Bike extends Vehicle {
}
#

and then you'd test

#
if (vehicle instanceof VehicleWithEngine hasEngine) {
   System.out.println(hasEngine.engineType);
}
else {
    // Do other stuff
}
#
  1. Don't declare engine-having on anything besides the implementing classes
#
abstract class Vehicle {}

enum EngineType {PETROL, DIESEL, ELECTRIC, UNKNOWN}

class Car extends Vehicle {
    EngineType engineType;
}

class Boat extends Vehicle {
    EngineType engineType;
}

class Bike extends Vehicle {

}
#

then you'd

#
if (vehicle instanceof Car car) {

}
else if (vehicle instanceof Boar boat) {

}
else {

}
#

secret 4th way

#

if you can make it so that no code actually has to access the engine type - i.e. put all the methods that want to use engine type on the interface/abstract class and use the field internally - then you don't need to worry about how to expose it

#
abstract class Vehicle {
    abstract void sayHi();
}

enum EngineType {PETROL, DIESEL, ELECTRIC, UNKNOWN}

class Car extends Vehicle {
    EngineType engineType;

    @Override
    void sayHi() {
        System.out.println("I am a car with " + engineType);
    }
}

class Boat extends Vehicle {
    EngineType engineType;

    @Override
    void sayHi() {
        System.out.println("I am a boat with " + engineType);
    }
}

class Bike extends Vehicle {
    @Override
    void sayHi() {
        System.out.println("I am a bike");
    }
}
#

then just

#
vehicle.sayHi();
#

but that only works if you can figure out all the things that want to use engineType and put them on the Vehicle abstract class/interface

upper trout
#

then call it wherever u need it