aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics compendium Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics compendium" Watch "Generics compendium" New topic
Author

Generics compendium

Ismael Upright
Ranch Hand

Joined: Feb 15, 2007
Posts: 166
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 ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14274
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Prem Vinodh
Ranch Hand

Joined: Mar 19, 2008
Posts: 33
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

Joined: Feb 15, 2007
Posts: 166
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

Joined: Mar 19, 2008
Posts: 33
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics compendium