The java api defines several generics and non-generics with the same name – like LinkedList etc. They are both resoled using the same statement import java.util.LinkedList;. But in one package, only one public class may be present in .java file(with the same name).
So can we get the same effect? I tried the following code:
package org.****.ai.ass1.part1;
import java.util.LinkedList;
public abstract class Test extends LinkedList { }
//public abstract class Test<E> extends LinkedList<E> { }
Without the comment, I get the following error : The type Test is already defined. However both the lines compile if I comment the other one. So how does the java api do it?
Due to the way Java implements generics (Google for type erasure),
LinkedList<T>andLinkedListrepresent exactly the same type. There is no way to declare two different types, one generic and one not, with the same fully-qualified name.