Now I have an existing class that I would like to refactor to be an Enum. The class currently extends another class which is from external library. As I still would like to benefit from some logics from that extended class meanwhile would like to refactor. How should it be done?
In Java, an enum
class cannot extend another class whereas it can implement interface. Or is it already wrong for the idea of refactoring it to be an Enum? Let me show it in the example code below.
Assume an existing class Existing is extending another class Parent and the Parent class is from an external library and it is NOT an interface.
class Existing extends Parent{
public static final Existing A = new Existing(...);
....
public static final Existing Z = new Existing(...);
public Existing(Srting attr1, String attr1){
super(attr1, attr2);
}
public Existing(String attr1){
super(attr1);
}
}
The idea is to have those static final fields to be Enums, e.g.:
enum NewDesign{
A(attr1, attr2),
B(attr1),
C(attr1, attr2)
//...;
//constructor etc.
//...
}
and possibly when needed, with new extra attr added as below:
enum NewDesign{
A(attr1, attr2, newAttr),
B(attr1, newAttr),
C(attr1, attr2, newAttr),
//...
//constructor etc.
//...
}
Enum cannot extend any class in java,the reason is, by default Enum extends abstract base class java. lang. Enum. Since java does not support multiple inheritance for classes, Enum can not extend another class.
Yes, Enum implements an interface in Java, it can be useful when we need to implement some business logic that is tightly coupled with a discriminatory property of a given object or class.
This is not possible. Enums cannot inherit from other enums.
The reason you can't extend Enums is because it would lead to problems with polymorphism.
If the main change that you need is to create an enum to get rid of static final fields, the easiest way to go about it is to create a different, enum type with initialized instances:
enum ExistingEnum {
A("attr1", "attr2"),
Z("attr");
private final Existing existing;
ExistingEnum(String attr1) {
this.existing = new Existing(attr1);
}
ExistingEnum(String attr1, String attr2) {
this.existing = new Existing(attr1, attr2);
}
public Existing getExisting() {
return existing;
}
}
Depending on how you're using your Existing
class currently, you may still be able to change it to an enum. In the following example, I expose an instance of Parent
on the enum, assuming code using Existing
can be changed to call Existing.A.getParent().parentMethod()
...
enum Existing {
A("attr1", "attr2"),
Z("attr");
private final Parent existing;
Existing(String attr1) {
this.existing = new ParentImpl(attr1);
}
Existing(String attr1, String attr2) {
this.existing = new ParentImpl(attr1, attr2);
}
public Parent getParent() {
return existing;
}
// only needed if logic is overridden
private static class ParentImpl extends Parent {
public static final Existing A = "";
public static final Existing Z = "";
public ParentImpl(String attr1, String attr2) {
super(attr1, attr2);
}
public ParentImpl(String attr1) {
super(attr1);
}
}
}
I think you can use composition:
public class Parent {
private String attr1;
private String attr2;
public Parent(String attr1, String attr2) {
this.attr1 = attr1;
this.attr2 = attr2;
}
public void doSomething() {
// do something.
}
}
// compose the class Parent
public enum NewDesign {
A("attr1", "attr2"),
B("attr1", "attr2")
;
private Parent parent;
NewDesign(String attr1, String attr2) {
this.parent = new Parent(attr1, attr2);
}
public void func() {
parent.doSomething();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With