• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt Abt Generic

 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is difference between

Basket<Fruit> f1=new Basket<Fruit>(); //1
Basket f2=new Basket(); //2
Basket f2=new Basket<Fruit>(); //3



class Basket<E> {
private E element;
public void setElement(E x) {
element = x;
}
public E getElement() {
return element;
}
}
class Fruit {}

Line 1 is generic no doubt abt it.
Line 2 is non generic no doubt abt it also.
Is line 3 same as non generic (raw)?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's "Abt" while we are about it? Try our Use Real Words note.

For an introduction to Generics try the Generics Tutorial.

Look for "generic types" and "raw types".
[ July 21, 2006: Message edited by: Barry Gaunt ]
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Line 3 is a raw type declaration as you can add any object in the f3.

f3.setElement(new Fruit()); legal
f3.setElement(new Dog()); legal

You can even add a dog into it. You will get a warning at the time of setting but no warning when you are getting object from Basket.

Naseem
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic