How does the datatype checking in a generic collection happen ? For example, take a look at the ArrayList below :
ArrayList<String> r1 = new ArrayList<String>();
r1.add("4");
r1.add("1");
r1.add("2");
r1.add("3");
How is the ArrayList<String>
linked to the method add()
? If I were to create my own class which is not a collection, how do i tie the generics parameter to make sure strict type checking occurs ?
Another example I observed in Android with the AsyncTask class :
private class SomeAsyncTask extends AsyncTask<String, Integer, Long> {
where String refers to the input, Integer to the progress and Long to the response. Where is this mapping done ? How do i create my own generic class and where do i define this mapping ?