• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics

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

l.add(a);
 
Alina Petra
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also compiled the code without error.
 
prashanth kumar
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try adding this line
l.add(a);

Cheers
Prashanth
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also compiled,the code without error.
 
prashanth kumar
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kindly look at the third post in this thread to know which code to compile

Regards
Prashanth
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic