Implementing Comparable, compareTo name clash: “have the same erasure, yet neither overrides the other”

痴心易碎 提交于 2019-11-29 03:38:18

The offending methods are:

@Override
public int compareTo(Real other) { ... }

public int compareTo(Object other) { ... }

These methods have the same erasure, meaning that once the compiler strips out generic type information, there will no longer be a way to differentiate them at runtime.

Your options are to either remove the compareTo(Object other) overload, or for Real to implement Comparable<Object>.

Since it looks like the implementations of all your compareTo overloads just instantiate a new Real and pass it to compareTo(Real), I'd suggest removing them and leaving that conversion up to the caller:

Real real = ...;
Object compared = ...;

Real comparedAsReal = new Real(compared);
int result = real.compareTo(comparedAsReal);

Since you want to be able to compare Real object to Object, you may just replace the implements Comparable<Real> with implements Comparable<Object>. This would be consistent with Comparable<T> javadoc which says that <T> the type of objects that this object may be compared to.

Then you just have to change your current code to :

// No more @Override
public int compareToReal(Real other)
{
  // Logic.
}
public int compareTo(char givenValue) 
{ return compareToReal(new Real(givenValue)); }
public int compareTo(char[] givenValue) 
{ return compareToReal(new Real(givenValue)); }
public int compareTo(char[] givenValue, int offset, int count) 
{ return compareToReal(new Real(givenValue, offset, count)); }
public int compareTo(double givenValue) 
{ return compareToReal(new Real(givenValue)); }
public int compareTo(float givenValue) 
{ return compareToReal(new Real(givenValue)); }
public int compareTo(int givenValue) 
{ return compareToReal(new Real(givenValue)); }
public int compareTo(long givenValue) 
{ return compareToReal(new Real(givenValue)); }
@Override
public int compareTo(Object other) 
{ return compareToReal(new Real(other.toString())); }

This is a side effect of Java generics type-erasure.

You are implementing a generic interface, Comparable, but this its unique method, once generic type erased will become compareTo(Object), hence it clashes with your own compareTo(Object).

Here is a minimal code to reproduce:

class Real implements Comparable<Real>
{
    public int compareTo(Object o)
    {
        return 0;
    }

    @Override
    public int compareTo(Real o)
    {
        return 0;
    }       
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!