• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

scjp generic question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

I'm completely new to java generics.

can any one help me with this question?




The following program contains two compilation errors, at what lines?




thanks

Payam
 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there isan error on line 10...16,17,18 look fine to me...20,21,22 look fine too....
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well...sorry my answers are wrong...sorry got them wrong....let me check why 21 and 22 are wrong
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tester.java:17: incompatible types
found : java.util.List<capture#674 of ? extends Chewable>
required: java.util.List<Chewable>
list2 = printSize(list2); // Line 21
^
Tester.java:18: incompatible types
found : java.util.List<capture#876 of ? extends Chewable>
required: java.util.List<Meat>
list3 = printSize(list3); // Line 22
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i think this is because list2 has a generic type of Chewable but we are assigning it a list of some other generic type...same is the case with list3......but if we had list2 and list3 without any geneircs then we would have got only warnings as then these would be non legacy codes...

am not sure...please someone confirm
 
Payam Rastogi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raju

it seems your answer is correct according to the question source:

http://www.javachamp.com/questions/proper-instantiation-of-java-generic-collection.xhtml

Raju, I am not sure that I got you right,

is the problem here in the return type of method printSize()?

Best.

Payam
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup..you are returning a List<? extends Chewable> from the method....so you can assign to a list which has a generic of type <? extends Chewable> or list without any generics.....without any generic would work becaue generic codes are allowed with non generics codes but you get warnings in such cases
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In generics you must rememeber that if you have a List<class> reference it can only ever refer
to a List<class>, not a List which is a sub-type of class or a list which has the wild card character ?
(although for backward compatibilty as Raju says, it can refer to a an old-fasioned non-generic list,
but you will get a compiler warning).In this case the return of value of the method is a List<? extends Chewable>,
which is being assigned to a List<Chewable> and List<Meat>, which is illegal

Martin.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic