wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics Doubt" Watch "Generics Doubt" New topic
Author

Generics Doubt

Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 118
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


SCJP 6 83%
Working on SCWCD/OCPJWCD
Madhukar Ojha
Ranch Hand

Joined: Mar 21, 2007
Posts: 71
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.


SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
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 ]

SCJP Java 6 (98%) - Story, SCJA (88%) - Story
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 118
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?
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Find this link, It clears all doubt regarding generics !


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 118
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 !
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

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 ]

SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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

Joined: Mar 15, 2008
Posts: 210

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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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

Joined: Mar 15, 2008
Posts: 210

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

Joined: Feb 19, 2008
Posts: 2902
    
    1

Ok, I got it, I need to work hard on generics .

Thanks for the help .
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 118
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 !!
 
jQuery in Action, 2nd edition
 
subject: Generics Doubt
 
Similar Threads
Generics
Generics
Need Explanation for a question in Generics
Generics Question
Why is the code giving compiler error