• 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
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Generics :Apart from "null" no input to a method is compilable

 
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry for flooding the forum with questions (i think i asked 2-3 on the same topic same day )

here my class is of Generic Type but the object that i am creating can only be of subtype of Number.
on creating this object i am trying to call the Generic Method i created for this class. but i am not able to call the method when i am passing the Integer. i can pass only null values, is it not strange?

 
Marshal
Posts: 79706
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know offhand, but it would help if you told us the full details including the full error message.
Have you been through the Java� Tutorials? Have you found Angelika Langer's FAQ?

But the only generic type which can encompass both an Integer and a String is <Object>.
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the error that i am getting while compiling this file is as below

"The method hello (capture of ? extends Number) in the type Test (capture of ? extends Number)is not applicable for the arguments Integer".

yeah for Integer and String we can use Object but why i cant pass any thing other than the Integer. i can pass only null values strange
 
Sheriff
Posts: 22802
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"? extends Number" means that it can in fact be a Test<Double> - and therefore accept only Doubles and not Integers.
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-------------------------Rob Prime wrote---------------------------
"? extends Number" means that it can in fact be a Test<Double> - and therefore accept only Doubles and not Integers.
--------------------------------------------------------------------
both Integer and Double are child classes of Number then why only Double is accepted and not Integer. Any way i tried with Double also for the same code but still got the similar error for Double as i got for Integer.





or i am misunderstanding some rule in Generics
 
author
Posts: 23956
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

but still got the similar error for Double as i got for Integer.



"? extends Number" means that it can in fact be a Test<Float> - and therefore accept only Float and not Doubles. Hence, it won't let you add Doubles.

Now, in case you try out floats...

"? extends Number" means that it can in fact be a Test<Short> - and therefore accept only Short and not Floats. Hence, it won't let you add Floats.

I'll let someone else answer, if you ask about shorts...

Henry
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is what i said .......it should accept some thing but apart from null I am not able to pass any parameter to "hello" method.
 
Henry Wong
author
Posts: 23956
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

Originally posted by Tanu Gulati:
That is what i said .......it should accept some thing but apart from null I am not able to pass any parameter to "hello" method.




Okay, you just didn't get it... so, let's try a straight explanation with all levity aside.

"? extends Number" means an *unknown* type that extends the Number class type. The compiler doesn't know what type it is, and at the same time the compile must type check anything that is added.

So, the only way the compiler will let anything to be added is to guarantee that the type being added is the correct type. This isn't possible because the compiler doesn't know what type it is, and there isn't a type that IS-A all the types that extend the number type.

So, it doesn't allow any type of object to be added.

The null is an exception to this. A null can be assigned to any type. Hence, it can be added to the container, no matter what type it is. Hence, it is legal to pass the null.

Henry
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So, it doesn't allow any type of object to be added.



But i am not trying to add anying i am creating an object of test and then i am calling a method and passing (Integer or Double) as an argument.

there isn't a type that IS-A all the types that extend the number type.



second point is i am passing a Double which IS-A Number. my all concepts are getting destroyed because of this code.
 
Henry Wong
author
Posts: 23956
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

But i am not trying to add anying i am creating an object of test and then i am calling a method and passing (Integer or Double) as an argument.



Change the word "add" to "work with"... I forgot that this your class, and not a collections class. The explanation still applies.


second point is i am passing a Double which IS-A Number. my all concepts are getting destroyed because of this code.



You have to pass "an unknown type that extends Number". Is a Double IS-A "unknown type that extends Number"? No, it is not guarranteed. It *may* be, but it might not be either.

Unfortunately, I don't know how many more ways to explain this. And if you concepts are conflicting with is, maybe it should be "destroyed" -- and you should take a fresh look at it.

Henry
[ November 28, 2008: Message edited by: Henry Wong ]
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i searched more i think you are Correct. the answers on this page made me understand with some more examples.please have a look

http://www.javabeat.net/javabeat/scjp5/mocks/Generics-part2-7questions.php

thanks Henry
 
I claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic