Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Generics

 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
KB chapter7 question 6


If the below change is made,


then ,its again a error,because
Wolf class breaks because its munch(Sheep)method no longer fulfills the contract of Carnivore.


So does that mean that if one class interprets E as a particular type,then EVERY OTHER class should interpret E as the same type? ?

 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Sheep extends Plant,

But

class Wolf extends Animal implements Carnivore<Sheep>

that means Carnivore<Sheep>==Carnivore<E extends Plant>.
But contract says that
interface Carnivore<E extends Animal> extends Hungry<E> {}.

So , it is not valid.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok..got it
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James just concentrate on this line

interface Carnivore<E extends Animal> extends Hungry<E> {}

1) You can see Carnivore type parameter is : E extends Animal

2) Now our case:

class Sheep extends Plant implements Carnivore<Wolf>

You can see here: Sheep extends Plant, so Sheep is a Plant now.

3) Now Wolf contract.
class Wolf extends Animal implements Carnivore<Sheep>

you can see here : Carnivore<Sheep>
and Sheep is now Plant as in the case 2.
so it becomes Carnivore<Plant>.
Fine but case 1 contract says Carnivore should take Animal or subtype of Animal.
interface Carnivore<E extends Animal>

So this case is not possible, as it is breaking the contract of interface Carnivore<E extends Animal>
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou... thing are clear now...
 
Sheriff
Posts: 9690
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to make it easier to understand, I have modified the code a bit

 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more question from the book.

Given a method declared as
public static <E extends Number> List<E> process(List<E> nums)

// INSERT DECLARATIONS HERE
output = process(input);


they say that the following statement cannot be used,because The return type of process is definitely declared as a List, not an ArrayList
ArrayList<Integer> input = null;
ArrayList<Integer> output = null;

I thought, since ArrayList implements List,why cant we use this. isn't it something like List<Integer> fun= new ArrayList<Integer>();
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayList<Integer> input = null;
this line is possible as it will make
List<Integer> nums=input;(means new ArrayList<Integer> input=null;)

ArrayList<Integer> output = null;
this is not possible as process() function return type is List.
so output=process() will result in

ArrayList<Integer> output = reference type of List<Integer>;

And subclass reference cannot be assigned superclass reference without casting.
 
Ankit Garg
Sheriff
Posts: 9690
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The return type of process method is List not ArrayList. So the return will look like this

ArrayList<Integer> output = new List<Integer>();

This is not allowed as List cannot be cast to ArrayList without an explicit cast.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohh... I am sorry guys for such a silly question.Generics and collection are confusing me a lot. :evil: .
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go slowly James, bump is ahead.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic