Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generic code

 
vikram choudhary .
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Above code compiles without warning why?
can any one explain?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Above code compiles without warning why?


Sorry, I cannot read your mind. What warnings are you expecting and on what lines?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic