The Artima Developer Community
Sponsored Link

Java Answers Forum
A quanstion about generics

1 reply on 1 page. Most recent reply: Jul 9, 2009 4:39 PM by Darko Latkovic

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Robinson Emmanuel

Posts: 1
Nickname: robinson
Registered: Oct, 2008

A quanstion about generics Posted: Oct 17, 2008 7:50 PM
Reply to this message Reply
Advertisement
Here is an example in 'Thingking in Java 4e':

public class GenericWriting {
static <T> void writeExact(List<T> list,T item){
list.add(item);
}
static List<Apple> apples = new ArrayList<Apple>();
static List<Fruit> fruit = new ArrayList<Fruit>();
static void f1(){
writeExact(apples, new Apple());
// writeExact(fruit, new Apple()); // Error:
//Incompatible types:found Fruit,required Apple
}
static <T> void writeWithWildcard(List<? super T> list,T item){
list.add(item);
}
static void f2(){
writeWithWildcard(apples, new Apple());
writeWithWildcard(fruit, new Apple());
}

public static void main(String[] args) {
f1();
f2();
}
}

class Fruit{

}

class Apple extends Fruit{

}

Bruce Eckel said "writeExact(fruit, new Apple());" is an error,but I run it rightly,could somebody tell me why?
Thank you very much.


Darko Latkovic

Posts: 9
Nickname: darko
Registered: Jul, 2009

Re: A quanstion about generics Posted: Jul 9, 2009 4:39 PM
Reply to this message Reply
Works fine for me as well. My Java version is 1.6.0_13.
Maybe it was an issue with some previous versions.

Flat View: This topic has 1 reply on 1 page
Topic: send multiple message Previous Topic   Next Topic Topic: Multiple persistence Unit

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use