I have a simple program like this:
package test;
public class TestGenericsInheritance {
public static void main(String[] args) {}
public static abstract class A<Q>{
public void foo2(Q obj){}
public abstract <T> void foo(T obj);
}
public static class C extends A<Object>{
@Override
public <T> void foo(T obj) {}
}
public static class B extends A{
@Override
public <T> void foo(T obj) {}
}
}
As you can see It does absolutely nothing. Compilation of this program files on java 1.6 and 1.7 with following error:
/D:/Projects/.../test/TestGenericsInheritance.java:[24,19] test.TestGenericsInheritance.B is not abstract and do es not override abstract method foo(java.lang.Object) in test.TestGenericsInheritance.A /D:/Projects/.../test/TestGenericsInheritance.java:[27,25] name clash: foo(T) in test.TestGenericsInheritance .B and foo(T) in test.TestGenericsInheritance.A have the same erasure, yet neither overrides the other /D:/Projects/.../test/TestGenericsInheritance.java:[26,9] method does not override or implement a method from a supertype
Classes C and B are semantically identical, however class B doesn't recognize method foo as implementation of A#foo. To make this code compliant I have to implement method A#foo in class B with following signature:
public void foo(Object obj)
My question is, why my program doesn't compile? Generic types Q and T are completely independent, so why I may implement generic function A#foo only when I explicitly specify generic type Q for the inherited class A?
Thanks