Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generic methods

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In the following code
import java.util.Collection;
import java.util.ArrayList;

public class TestGenerics {

static <T> void fromArrayToCollection(T[] a,Collection<T> c){

for(T b:a)
c.add(b);
System.out.println("Added");
}

static <T> void fromCollectionToCollection(Collection<T> a,Collection<T> c){

System.out.println("Added");


}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Integer[] oa = new Integer[10];
Collection<Number> cn= new ArrayList<Number>();
Collection<Integer> ci= new ArrayList<Integer>();
fromArrayToCollection(oa,cn);
fromCollectionToCollection(ci, cn);
}

}

why am I getting compilation error in fromCollectionToCollection(ci, cn); method but not in fromArrayToCollection(oa,cn);

However in both cases the T type being given is different.But in one case it works and in other it does not.

thanks in advance
mayur
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the error you are getting !
 
mayur dhawan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please find the error
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method fromCollectionToCollection(Collection<T>, Collection<T> in the type TestGenerics is not applicable for the arguments (Collection<Integer>, Collection<Number>

at com.generics.TestGenerics.main(TestGenerics.java:42)
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure about this , but as you are using the same parameter <T> , for both the classes , it dont work !! (Though Integer class is sub class of that Number class)

This code works fine ,


You can find a great information about Generics HERE
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Sagar,
Thanks for the link.
It was good
 
mayur dhawan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.
But I am still unclear on the answer.CAn you please elaborate it .

The change from T to D works but the same change is not required in the method fromArrayToCollection().


thanks in advance
mayur
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The answer is still unclear. Can some one throw more insight on the concept as why it is not compiling.
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

The method
<T> void fromCollectionToCollection(Collection<T> a,Collection<T> c)
is declared to take two collections of the same type. For this reason you cannot pass in a Collection<Number> and Collection<Integer>. It should be fairly obvious that this cannot work. What would you suggest that the compiler should deduce for the type parameter T? Number? Integer? Something else? Either way, the method cannot be invoked with the arguments Collection<Number> and Collection<Integer>. Mind, Collection<Integer> is NOT a subtype of Collection<Number>.

The invocation of the other method
<T> void fromArrayToCollection(T[] a,Collection<T> c)
works because arrays (different from parameterized types) are covariant. The compiler concludes that T must be Number and then the first argument must be of type Number[]. Since Integer[] is a subtype of Number[] it works.

The key point is that arrays are covariant and parameterized types are not.

This is the reply from Angelika Langer

 
Seriously Rick? Seriously? You might as well just read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic