• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Generics compendium

 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread is supposed to be a little compendium about generics. There are lot's of fact and personally I am getting confused every time I don't see the generics for more than one week
Add here informations about generics but keep it short - don't put explanations, just facts. And don't forget about simple examples.




1. List<CharSequence>

It means that this list contains objects of class CharSequence.

We can retrieve only objects of class CharSequence from such List.
We can put only objects of CharSequence class into that List.



2. List<? super CharSequence>

It means that this list contains objects of either CharSequence or some class that is a superclass of CharSequence.

We cannot retrieve anything from such List.
We can put only objects of CharSequence class into that List.



3. List<? extends CharSequence>

It means that this list contains objects of some class that is either CharSequence or some subclass of CharSequence.

We can retrieve only objects of class CharSequence from such List.
We cannot put any objects into that List.



4. List<?>

It is the same as: List<? extends Object>.
It means that this list contains objects of some class that is either Object or some subclass of Object.

We can retrieve only objects of class Object from such List.
We cannot put any objects into that List.
[ May 19, 2008: Message edited by: Ismael Upright ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can find more than you ever wanted to know about generics in Angelika Langer's Generics FAQ. Note, that document goes really into all the details, probably more than you need to know for the SCJP exam.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ismael

quote
-------------------------------------------------------------------------

List<? super CharSequence>

It means that this list contains objects of either CharSequence or some class that is a superclass of CharSequence.

We cannot retrieve anything from such List.
We can put only objects of CharSequence class into that List.
-------------------------------------------------------------------------

What does we cannot retrieve anything from such a list mean?
I did not understand because if I can't retrieve something from a list then
there is no use of the list.

quote
--------------------------------------------------------------------------
4. List<?>

It is the same as: List<? extends Object>.
It means that this list contains objects of some class that is either Object or some subclass of Object.

We can retrieve only objects of class Object from such List.
We cannot put any objects into that List.

--------------------------------------------------------------------------

What does 'We can retrieve only objects of class Object from such List'
mean. Is it that if I add a Dog or a Cat into such a list then I can't retrieve them or that whatever is put into it when retrieved would come out as an Object which we have to type cast back to get the original object.


Please explain I am trying to understand Generics and I am really struggling.
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quote: What does we cannot retrieve anything from such a list mean?

Answer: it means that you cannot call any method on that list that returns its element.



Quote: I did not understand because if I can't retrieve something from a list then there is no use of the list.

Answer: Hmm... I think that you have to remember that it is possible to have two different references to one physical list:

List<String> writeAndRead = new ArrayList<String>(); //1
List<? super String> writeOnly = writeAndRead; //2

Assume that you are a boss in a big company and you want to create an application so every employee of yours could report bad thing about others :>
What do you need? Some structure to store reports that you could read and remove reports you already seen. At the same time you should enable that structure to others, but of course only for writing, so that no one can see who reported who.
You can do that by creating the List as above and provide reference number 1 to yourself only and reference number 2 to all employees from your company.



Quote: What does 'We can retrieve only objects of class Object from such List' mean. Is it that if I add a Dog or a Cat into such a list then I can't retrieve them or that whatever is put into it when retrieved would come out as an Object which we have to type cast back to get the original object.

Answer: It means that whatever is put into it when retrieved would come out as an Object which we have to type cast back to get the original object.
 
Prem Vinodh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ismael

Really struggling a lot to understand Generics and your compendium is really helping me to understand Generics better.

But I still have some doubts.


quote
-------------------------------------------------------------------------

List<? super CharSequence>

It means that this list contains objects of either CharSequence or some class that is a superclass of CharSequence.

We cannot retrieve anything from such List.
We can put only objects of CharSequence class into that List.
-------------------------------------------------------------------------

Quote : Refering the above quote : "We cannot retrieve anything from such List."

I can still retrieve elements from the list.

Please refer to the program below :

--------------------------------------------------------------------------
import java.util.*;

class SuperAnimal
{
String sname = null;

public String getSname() {
return sname;
}

public void setSname(String sname) {
this.sname = sname;
}
}

class Animal extends SuperAnimal
{
public void addAnimal(List <? super Animal> animals) {
//animals.add(new SuperAnimal()); // 1
animals.add(new Animal());
animals.add(new Dog());

for(int iCnt = 0; iCnt < animals.size(); iCnt++) {
System.out.println("Animals["+iCnt+"] ::"+animals.get(iCnt));
}
}
}

class Dog extends Animal
{
String name = null;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

public class GenericsSuper
{
public static void main(String[] args) {
Animal animal = new Animal();

List<? super Animal> animal1 = new ArrayList<SuperAnimal>();
animal1.add(new Dog());
animal1.add(new Animal());
//animal1.add(new SuperAnimal()); // 2
animal.addAnimal(animal1);

List<? super Animal> animal2 = new ArrayList<Animal>();
animal2.add(new Dog());
animal2.add(new Animal());
//animal2.add(new SuperAnimal()); // 3
animal.addAnimal(animal2);

//List<? super Animal> animal3 = new ArrayList<Dog>();

List<? super Animal> animal4 = new ArrayList<Object>();
//animal4.add(new Object()); // 4
animal.addAnimal(animal4);

}
}
---------------------------------------------------------------------------

Q1) When I run the above program I am able to retrieve elements from the list and print them, but according to what you said
Quote :
-------------------------------------------------------------------
'List<? super CharSequence> '

We cannot retrieve anything from such List.
We can put only objects of CharSequence class into that List.
-------------------------------------------------------------------

Q2) The addAnimal method in the Animal class can take a List<? super Animal>, which means it can take a list that is Animal or anything above animal. But // 1 in the same method shows error? I thought it should have taken the SuperAnimal object. Similary with // 2 and // 3
Q3) Refer the line just above // 4 which is this
List<? super Animal> animal4 = new ArrayList<Object>();
which means it can take an object (because Object is parent to all other classes and my addAnimal method takes superclass of Animal) so when I trying to add an object to the list it shows error. why?


Please explain and help me?

[ May 25, 2008: Message edited by: Prem Vinodh ]
[ May 25, 2008: Message edited by: Prem Vinodh ]
 
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic