• 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
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Generics question

 
Ranch Hand
Posts: 159
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

this gives a compiler fault at

any idea whats the explanation ?
I know its a little bit tricky,but Integer extends number so why can't I pass it into the method ?



thanks!


regards,
Mark
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark wrote:

Integer extends number


Yes. But there is no polymorphy with generic types. But this isn't the problem here. It's because the type is unknown.
Even this will not compile:




Whereas this will compile and print "null":


Yours,
Bu.

[ December 14, 2006: Message edited by: Burkhard Hassel ]
[ December 14, 2006: Message edited by: Burkhard Hassel ]
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The following declaration is perfectly true which says that any collection that is a subtype of Number and typed for <Number> or any of its subclass is the right candidate to refer to.

But using this signature one can also pass(we dont know what the method can do with the passed collection..it can alter it) Short,Long,Float and so on.. and will break the collection,so as long as we dont add any object to this collection it is perfectily true.

To change the program to be a perfect one we need to change it as

In this case one can pass Integer only ,not the Short,Byte,Long...not Object too.However the polymorphic type gp refers to some other collection suppose typed for Object then also it is perfectly legal to add object of Object in it.. like

[ December 14, 2006: Message edited by: Sanjeev Kumar Singh ]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark

Here are two code samples similar to yours that do compile. (Well at least they do on my box!)

Code sample 1
This shows how to make a generic class more specific.



Code Sample 2
Shows one example of how to use polymorphism with Generics. To quote from K & B in the two minute drill for Generics(Objective 6.4) "Polymorphic assignments applies only to the base type not the generic type parameter."




All the best

Dave
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The declaration
GenericP<? extends Number> gp
says that anything can go in this gp which passes the "is a" test for Number. You created the class GenericP to hold only Integer (new GenericP<Integer> , if the method call gp.aMethod would have succeeded then anything which is a Number could be passed to that method which is supposed to accept only Integer and all your type safety will be lost, to avoid such situation the above code is illegal. Following is just a variation of the declaration
GenericP<? super Number> gp = new GenericP<Number>();
gp.aMethod(new Integer(5));
gp.aMethod(new Double(5));
in which case your type safety is assured and the compiler is also happy.

Hope I am clear.
 
Mark Uppeteer
Ranch Hand
Posts: 159
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
thanks for the answers!
The goal of my answer was to try to understand how it works.
I know how to write a regular generic class, but since you can do all this strange stuff, I'm trying to understand to point of why you can do it.

to Sanjeev Kumar Singh: this problem has nothing todo with collections
to Burkhard Hassel: yes, apperantly it is notpossible to create an argument that fullfills the need of the method. Null avoids the problem a bit.
to David Grindley : yes, those are the cases that generics work normal
to Ahmed Khan Mohammed : I like your explanation , but it doesn't compile either.


regards,
Mark
 
Evildoers! Eat my justice! And this tiny ad's justice too!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic