Hi Vikram,
Actually your confusion runs around last couple of lines of
your code;
class NewClass{
public static void main(String[] args) {
LinkList<String> s=new LinkList<String>();
LinkList<Object> list1;
LinkList<String> list2;
//list1=list2; // this line generates compilation error incompatible types
}
}
Parameterized (generic) assignment doesn't happen in that way.
In one line,
"polymorphism applies to base type not parameterized type."
LinkedList<
String> list1 = new ListedList<String>();
Here LinkedList is the base type and <String> is parameterized type.
You can apply the quoted line with the following line:
List<String> list2 new LinkedList<String>();
Here List and LinkedList are base types so no problem.
Polymorphism (parent class/interface type ref variable holding object of subtype/implementing class type object) applies to base type;
You can't write like:
List<Object> list3 = new LinkedList<String>(); //Line #1
under the impression that String IS-A(n) Object. That would be wrong.
Actually these type informations are erased finally. They are only to restrict the programmers so that nothing wrong can be inserted to the List.
After type erasure the Line #1 would become
List list3 = new LinkedList();
[ April 29, 2007: Message edited by: Chandra Bhatt ]