Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name Clash compile error when compiled in java 7 but works fine in java 5

public interface Expression {

}

public interface ArithmeticExpression extends Expression {

}


public class StaticMethodDemo {
  public static void print(Expression e) {
    System.out.println("StaticMethodDemo");
  }

  public static List<Expression> convert(
        Collection<? extends Expression> input) {
    return null;
 }
}


public class StaticMethodChild extends StaticMethodDemo {

    public static void print(ArithmeticExpression e) {
    System.out.println("StaticMethodChild");
   }

   public static List<ArithmeticExpression> convert(
        Collection<? extends ArithmeticExpression> input) {
    return null;
  }
}

Above code compiles in java 5 but not in java 7 why? In java 7 it gives "Name clash: The method convert(Collection) of type StaticMethodChild has the same erasure as convert(Collection) of type StaticMethodDemo but does not hide it"

like image 909
Dragon Avatar asked Sep 25 '13 07:09

Dragon


1 Answers

Besides stonedsquirrel's answer, even if the methods were not static, you would get the same error.

This is because of Type Erasure, you would be trying to override convert with an incompatible type.

Check the following answer for a nice explanation.

like image 59
aayoubi Avatar answered Oct 07 '22 15:10

aayoubi