0

Lets say I have class

public class ParentClass {
    private String field1;
    private String field2;
    private String field3;
    private List<AnotherParentPojo> evses;
}

public class AnotherChildPojo extends AnotherParentPojo {
    private String fieldA;
    private String fieldB;
    private String fieldC;
}

public class ChildClass extends ParentClass {
    private List<AnotherChildPojo> evses;
}

However this leads to the error "both methods have same erasure, yet neither overrides the other" in the ChildClass. I am aware its not possible to override a field but is there any way to achieve this without having to change ParentClass or AnotherParentPojo? I only have control over the ChildClass and AnotherChildPojo

hradecek
  • 2,455
  • 2
  • 21
  • 30
Ahmed
  • 121
  • 6
  • 18
  • The given example is not correct. Please add the methods as well – sarcode Dec 16 '21 at 19:21
  • Does this answer your question? [Java name clash, have the same erasure, neither hides the other](https://stackoverflow.com/questions/14002965/java-name-clash-have-the-same-erasure-neither-hides-the-other) – sarcode Dec 16 '21 at 19:23
  • I am using @Data annotation, its just simple setters and getters – Ahmed Dec 16 '21 at 19:30

2 Answers2

1

As you mentioned @Data annotation I assume, you are using lombok [1].

The issue issue that ParentClass and its child ChildClass basically declare same setters/getters but with different type erasure.

Even though you cannot see them, they are there (lombok magic).

In order to understand what is happening, I advise you to de-lombok the actual code.

public class ParentClass {

...

    public List<AnotherParentPojo> getEvses() {
        return evses;
    }

    public void setEvses(List<AnotherParentPojo> evses) {
        this.evses = evses;
    }

...

}
public class ChildClass extends ParentClass {
...

    public List<AnotherChildPojo> getEvses() {
        return evses;
    }

    public void setEvses(List<AnotherChildPojo> evses) {
        this.evses = evses;
    }
...
}

Solution

Solution might be to just rename one of the "clashing" fields or turn off generation of setter and getter using @Setter(AccessLevel.NONE) @Getter(AccessLevel.NONE) annotations for particular field.


[1] https://projectlombok.org/

hradecek
  • 2,455
  • 2
  • 21
  • 30
  • So we are following a contract and the response returned must have evses so renaming the field may not be an option. What would turning off setters and getters achieve? I still need to be able to access the evses – Ahmed Dec 16 '21 at 19:54
  • By turning off setter and getter, you can make your own getter and setter for particular field in order to avoid name clash. – hradecek Dec 16 '21 at 19:57
  • @Ahmed, why renaming is not an option? `ChildClass` inherits `getEvses/setEvses` from `ParentClass` with `List` and has its own `getEvsesChild/setEvsesChild` dealing with `List` data. It is not a breaking change for interface but rather an extension. – Nowhere Man Dec 16 '21 at 20:11
  • My mistake, I misunderstood. I was able to rename the getter and setter method name so it doesn't conflict with the parent's class getter/setter and kept the field name as evses and it worked smoothly. Thanks! – Ahmed Dec 16 '21 at 20:36
0

If Lombok's @Data annotation is applied to both ParentClass and ChildClass, appropriate getters/setters are generated:

// ParentClass
public void setEvses(List<AnotherParentPojo> list) {...}
public List<AnotherParentPojo> getEvses() {...}

and the name clash occurs in ChildClass as it is inherited from ParentClass

// ChildClass
public void setEvses(List<AnotherChildPojo> list) {...}
public List<AnotherChildPojo> getEvses() {...}

If access to ChildClass is available, the simplest solution would be just to rename its evses field, thus renaming the getters/setters and resolving the name conflict.

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42