silly questions silly questions -4 years ago 59
Java Question

Erasure of method is the same as another method in type

Following are two methods in a class

class Developer and Student are different and not share any parent.

Both following method has same code and method called on both object have same name ex. getStartDate.

class hierarchyValidator {
private LocalDate[][] getArrayOfTimespan(List<Developer> developers)
{
//here i have to call some method on developer from list
LocalDate startDate = developer.getStartDate();
....
}

private LocalDate[][] getArrayOfTimespan(List<Student> students)
{
//here i have to call some method on student from list
LocalDate startDate = student.getStartDate();
.....
}
}


It is showing error Erasure of method is the same as another method in type

I understand the reason behind error from other post on same site.

How to re-factored it so that error wont be there and code will be clean?

Answer Source

Your problem is due to type erasure: the parametrized type information in your Lists is erased at runtime, hence the methods have a virtually identical signature and your code cannot compile.

In order to solve your problem, here's a solution that generalizes the common features of Developer and Student, thus only requiring one getArrayOfTimespan method for both given parametrized types:

// common interface to Student and Developer classes
interface Datable {
    LocalDate getStartDate();
}
// dev class implementing "datable"
class Developer implements Datable {
    @Override
    public LocalDate getStartDate() {
        // TODO Auto-generated method stub
        return null;
    }
}
// student class implementing "datable"
class Student implements Datable {
    @Override
    public LocalDate getStartDate() {
        // TODO Auto-generated method stub
        return null;
    }
}
// parameter interpreted as list of super type to both dev and student
private LocalDate[][] getArrayOfTimespan(List<Datable> args)
{
    for (Datable d: args) {
        // TODO something
        LocalDate foo = d.getStartDate();
    }
    // TODO return something
    return null;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download