• 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

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
source: Generics Tutorial at java.sun.com



Question: will the following work properly?
AnimalHouse<?> house = new AnimalHouse<Cat>();
house.setAnimal(new Cat());

Answer 1c: 1. fails to compile
While the first line is acceptable � it is OK to define an instance of unknown type � the compiler doesn't know the type of animal stored in house so the setAnimal method cannot be used.


I did not understand the answer i feel it should work properly
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Milan,

Compilation will fail at



Because horse is instantiated with wild-card ?.
? reference gives you view mode.

You can't add any stuff using this reference.
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Milan,

to say the truth, because you used a wildcard on the type of the reference, it won't compile if you try to use any method that uses the E because you can't guarantee nothing!

So, you could do some things like a cast do the correct type, or a overload of the method. Here are two examples:



AND:



I made then separeted beucase the compiler wouldn't know the correct method to execute if i did both changes together. I'm hoping it can help in some way.

Raphael Rabadan
[ July 18, 2008: Message edited by: Raphael Rabadan ]
 
Milan Sutaria
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Madhukar & Raphael, it did clear my doubt but it did not understand Raphaels 2nd code. why should it invoke the 2nd setAnimal method and if it is casted to AnimalHouse<Cat> wi=ould invoke the 1st version of setAnimal.

I still find the Generics topic very obscure. I have been through K&B but some (mock test) questions seem to be beyond whats covered in book. Can anyone suggest some good link to refer to?
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Find this link, It clears all doubt regarding generics !
 
Milan Sutaria
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sagar. but i have couple of more doubts...

how does <? extends Animal> differ from <T extends Animal>? I think they both mean the same " any type that extends from class Animal or class Animal itself" But there are some conventions to it like you can use <T ...> for specifying for class but not <?...> Are there any other rules associated? ( you can add to ArrayList<T extends Animal> when it is an argument to a method)

Also, is the following code is right?

 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Milan Sutaria:


how does <? extends Animal> differ from <T extends Animal>? I think they both mean the same " any type that extends from class Animal or class Animal itself" But there are some conventions to it like you can use <T ...> for specifying for class but not <?...> Are there any other rules associated? ( you can add to ArrayList<T extends Animal> when it is an argument to a method)




As per my knowledge , what you said is perfectly fine and presently I have no information about other rules associated !

Originally posted by Milan Sutaria:

Also, is the following code is right?




and the above code is fine, but I am stuck with the example, where I called the "someMethod()" ,
Here is my code :


I am searching for answer , as soon as i got it, I ll post it here !
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
List<Number> l = someMethod(new ArrayList<Number>());

above method call won't compile because, the return type of method someMethod() is List<? super Integer>,

means it can return either List of Integers or List of Super type of Integers.for example it can return List<Integer> or List<Number> or List<Object> etc in runtime.
So we can not hold result in List<Number> that is why it won't compile.


<T extends Animal>

This kind of syntax is used to create generic class or generic method,the behaviour of the class or method will change according to the parameter T passesd.

<? extends Animal>

? called as wild card which is used only in references
[ July 20, 2008: Message edited by: ramesh maredu ]
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ramesh maredu:
Hi,
List<Number> l = someMethod(new ArrayList<Number>());

above method call won't compile because, the return type of method someMethod() is List<? super Integer>,

means it can return either List of Integers or List of Super type of Integers.for example it can return List<Integer> or List<Number> or List<Object> etc in runtime.
So we can not hold result in List<Number> that is why it won't compile.


[ July 20, 2008: Message edited by: ramesh maredu ]



Hi as per your explanation, I modified my code as :



Here I`m returning the same ArrayList<Number> object type and taking it into same type . But it still raise an error ! stating :

gen.java:16: incompatible types
found : java.util.ArrayList<capture of ? super java.lang.Integer>
required: java.util.ArrayList<java.lang.Number>
return list;
^
1 error



Help me ,
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Now problem is with method someMethod()

static ArrayList<Number> someMethod(ArrayList<? super Integer> list)
{
list.add(3);
return list;
}

Here you are trying to add some value to ArrayList<? super Integer> which is fine, but when you try to return the same list problem occurs,because of the same problem that i explained before.

either you have to modify code like below




or
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, your both codes are fine, they are working , you rocks !!,,

But , considering my code once again,



So my doubt is, at the compile time, why compiler not getting it, we are passing a wildcard parameter as "Number' and returning the same (Everything regarding class hierarchy everything is fine ). Why it throws error then,

It may some quirks in Java generics OR I simply forgot a basic Java fundamental concept ! Help me out ,

I`m learning java, so you may find my doubt silly !
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is problem in your method itself not with method call.

static ArrayList<Number> someMethod(ArrayList<? super Integer> list)
{
list.add(3);
return list; // returning 'Number'
}

here problem is return type,ArrayList<Number> can not hold ArrayList<? super Integer> as i explained before ArrayList<? super Integer> can be anything at runtime which super of Integer.
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I got it, I need to work hard on generics .

Thanks for the help .
 
Milan Sutaria
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ramesh thanks for your explaination. & sagar thanks to you too

i did find a couple of Links (including Generics whitepaper by Glad Broacha) which clears many doubts ( any also makes me hate generics to the limit for its zillion rules)(... how about sun starting an exam only on type parameters & generics which includes inheritance overriding polymorphism etc concepts with generics)

Generics Tutorial EXTRA for detail study

Tech Tips at sun.com
[chk the links at the bottom of this page]
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Milan Sutaria:
(... how about sun starting an exam only on type parameters & generics which includes inheritance overriding polymorphism etc concepts with generics)

Generics Tutorial EXTRA for detail study

Tech Tips at sun.com
[chk the links at the bottom of this page]



Right, Im too thinking on same, the Generics with legacy , The SUN engineers put us into little mess !



anyways, thanks for the links !!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic