This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
rimzy abdulkader
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Alina Petra
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not getting any error. What error are you referring to?
 
rimzy abdulkader
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to add

l.add(a);
 
Alina Petra
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alina Petra
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>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
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also compiled the code without error.
 
prashanth kumar
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding this line
l.add(a);

Cheers
Prashanth
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Venkat Sidh
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also compiled,the code without error.
 
prashanth kumar
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic