• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Generics Doubt

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI All,

Please explain me ,what is the error in the following code line numbers 21 and 22..

What my understanding is : In line 10,

List<? extends Chewable> printSize(List<? extends Chewable> list)

means, anything that extends Chewable type, that is Meat can be the return type of the Printsize..Line 22 Printsize return type is Meat ..So,what is the error in 22 ?

And also, explain for the line 21 error..


import java.util.ArrayList;
import java.util.List;


interface Chewable {}
class Meat implements Chewable {}


public class Tester {

public static List<? extends Chewable> printSize(List<? extends Chewable> list) { // Line 10

System.out.println(list.size());

return list;

}

public static void main(String[] args)
{

List<? extends Chewable> list1 = new ArrayList<Meat>(); // Line 16
List<Chewable> list2 = new ArrayList<Chewable>(); // Line 17
List<Meat> list3 = new ArrayList<Meat>(); // Line 18


list1 = printSize(list1); // Line 20
list2 = printSize(list2); // Line 21
list3 = printSize(list3); // Line 22
}

}


Thanks all..
SR
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sangeetha raj wrote:HI All,

Please explain me ,what is the error in the following code line numbers 21 and 22..

What my understanding is : In line 10,


means, anything that extends Chewable type, that is Meat can be the return type of the Printsize..Line 22 Printsize return type is Meat ..So,what is the error in 22 ?

And also, explain for the line 21 error..

Thanks all..
SR



The printSize() method returns a list of something that extends Chewable. The compiler actually doesn't know what is the type the list is of -- only that the type of the item that is something that extends chewable. The list3 reference variable is to a list of type Meat. And since there is no way for the compiler to confirm that the list returned by the printSize() method is a list of Meat, it generates an error.

Henry
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question can be derived into this scenario:




Correct me if I am wrong.
 
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's correct. A "List<? extends Chewable>" could turn out to be a "List<Gum>" at run time, so allowing it to be assigned to a List<Meat> variable is not correct.
 
sangeetha raj
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

Thanks for the Reply..


Since Meat class implements chewable, can't we say something that extends chewable could be Meat ? And also is there any error in Line 21 in my code..Please explain...

Paul - Thanks for the reply..I couldn't understand, how List < ? extends Chewable > could turn out to be a List <Gum> at run time..Please explain me..

Halen ma - Thanks Halen for the reply...


 
Paul Clapham
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sangeetha raj wrote:Paul - Thanks for the reply..I couldn't understand, how List < ? extends Chewable > could turn out to be a List <Gum> at run time..Please explain me.



It's not especially complicated. Here's an example:


 
Henry Wong
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sangeetha raj wrote:
Since Meat class implements chewable, can't we say something that extends chewable could be Meat ?



I guess it could be a list of Meat -- but "could be" is not good enough. That is like saying that Java should allow all integers variables to be converted to bytes implicitly, because it "could be" small enough to fit. Assignment conversion don't work like that -- for implicit casts to be allowed, it has to be confirmed to work.

sangeetha raj wrote:
And also is there any error in Line 21 in my code..Please explain...



Same reason, but a bit more subtle. A List of chewable is a list that can take any chewable -- this means that it can take Meat, Vegetable, Gum, etc. This is not the same as a List of Meat. If you assigned a list of meat to a list of chewable (allowed it), you break the list for meat, as you can use the list of chewable to insert items into the list of Meat that is not meat. Given this, line 21 breaks for the same reason as line 22.

Henry
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic