aspose file tools*
The moose likes Beginning Java and the fly likes Generics :Apart from Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generics :Apart from "null" no input to  a method is compilable" Watch "Generics :Apart from "null" no input to  a method is compilable" New topic
Author

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

Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

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?



thanks
Tanu Gulati
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
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

Joined: Oct 06, 2008
Posts: 113

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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

"? extends Number" means that it can in fact be a Test<Double> - and therefore accept only Doubles and not Integers.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

-------------------------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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18988
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

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
Sheriff

Joined: Sep 28, 2004
Posts: 18988
    
  40

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

Joined: Oct 06, 2008
Posts: 113

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
Sheriff

Joined: Sep 28, 2004
Posts: 18988
    
  40

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

Joined: Oct 06, 2008
Posts: 113

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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics :Apart from "null" no input to a method is compilable