aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics 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" Watch "Generics" New topic
Author

Generics

rimzy abdulkader
Greenhorn

Joined: Oct 04, 2006
Posts: 18
import java.util.*;
class A{

}
class B extends A{

}
class C extends B{
}
class T{
public static void main(String [] args){

List<? super B>l=new LinkedList<A>();

A a=new A();
B b=new B();
C c=new C();
l.add(c);



}


}


it gives error . why??


SCJP 5.0
Alina Petra
Greenhorn

Joined: Oct 17, 2006
Posts: 26
I'm not getting any error. What error are you referring to?
rimzy abdulkader
Greenhorn

Joined: Oct 04, 2006
Posts: 18
try to add

l.add(a);
Alina Petra
Greenhorn

Joined: Oct 17, 2006
Posts: 26
You can not add an object of type A to the list. You are allowed to add just objects of type B or subtypes of B to the list. Imagine that you instantiate your collection with new LinkedList<B>(). What would happen if you were allowed to add an object of type A to it?
prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162
I too am confused with this question
>>List<? super B>l=new LinkedList<A>();

The abobe declaration means that the list can accept B and any supertypes of B.And according to the code A is supertype of B??isnt it??

Thanks
Prashanth


SCJP1.5(86%)<br />SCWCD1.4(95%)<br />SCBCD1.3(92%)<br />IBM 252
Alina Petra
Greenhorn

Joined: Oct 17, 2006
Posts: 26
Yes, you are right. Your list (List<? super B> can take any list with the type being B or supertype of B. Your list could be LinkedList<B> or LinkedList<A>. But when you try to add something to the list, the type of the object that you add must pass the IS-A B relationship. Think what would happen if you added an object of type A and instantiated your list: List<? super B> l = new LinkedList<B>(). If you were allowed to add objects of type A to the list, you'd get a list of type B that would contain objects of type A. This is Not ok.
Venkat Sidh
Ranch Hand

Joined: Sep 30, 2006
Posts: 61
That means new LinkedList<A>(); can hold A,B,C but on left hand side List<? super B>l states that it is going to hold only B or subclass of B (or anything with B as a Super class)
prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162
>>List<? super B>l states that it is going to hold only B or subclass of B (or >>anything with B as a Super class)

I dont think the above statement is right...It means that it is going to hold B and any superclasses of B...

For your argument to be valid,the declaration should have been
<? extends B>

Let me know if iam wrong..

I still cant understand this anser for the above question.I believe i have a common problem with generics
Rgds
Prashanth
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I also compiled the code without error.
prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162
Try adding this line
l.add(a);

Cheers
Prashanth
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by prashanth kumar:
Try adding this line
l.add(a);

Cheers
Prashanth


I was speaking about the original problem.
Andrea Z�rcher
Greenhorn

Joined: Oct 06, 2006
Posts: 10
For your argument to be valid,the declaration should have been
<? extends B>


Hi Prashanth

It would be a bad idea to write <? extends B>. Upper-bounded wildcards result in read-only collections while lower-bounded wildcards result in write-only collections!

Andrea


SCJP 5.0
Venkat Sidh
Ranch Hand

Joined: Sep 30, 2006
Posts: 61
Prashanth,

I dont think the above statement is right...It means that it is going to hold B and any superclasses of B...


What I was saying is subclass of B that means anything with B as a super class.
Naveen Zed
Ranch Hand

Joined: Aug 11, 2006
Posts: 61
I also compiled,the code without error.
prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162
Kindly look at the third post in this thread to know which code to compile

Regards
Prashanth
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics
 
Similar Threads
Generics question from Exam Lab
Self Written Code on Generic Problem
SCJP 6 question
Get in if you like generics
Generics Question: