Java Generics wildcards and their limitations?

Wildcards bounds (upper and lower) are often mandatory for collections and classes returning objects of parameterized type.

Up vote 4 down vote favorite 1 share g+ share fb share tw.

I have a two part question My book states this "If the wildcard is specified without an upper bound, then only the methods of type Object can be invoked on the values of the wildcard type" I have no idea what this could be mean. What does this mean? Also what are the limitations place on wild card types (unbounded and bounded)?

For instance if I have a reference to MyClass or MyClass, what methods am I not allowed to call through that reference. I don't understand what the wild card allows or disallows me to do, which is probably why I don't understand the quote from the book. Thanks you :-) I have an example for the second part class SomeOtherClass { ... } class MyClass { ... } class Test { public static void main(String arg) { MyClass myClass = new MyClass() // for instance what does the wild card reference limit me to in any way.

In a general sence. } } java generics link|improve this question edited Mar 20 at 8:11 asked Mar 20 at 7:48rubixibuc1,02219 90% accept rate.

Wildcards bounds (upper and lower) are often mandatory for collections and classes returning objects of parameterized type. You'll often hear about PECS, which means "Producer extends, Consumer super". I suggest you to read the answer to this question, to avoid duplicating answers.

To be more precise, when you define your wildcard with , then you're telling the compiler that the wildcarded object is at least of type TheClass. Therefore, you are able to use this object like an instance of TheClass, and call any method this type proposes. Now, when you define your wildcard as , you're telling the compiler that your wildcarded object type is implemented or extended by the TheClass type.

It means that the object type may not be TheClass, but that a TheClass object can be used as an instance of your wildcarded reference. Therefore, you cannot call anything on that object, since its type is only known at runtime, but you can pass the object to methods waiting for a wildcarded object. Examples: private void foo(List list) { Object o = list.

Get(0); // ok list. Add(new Object()); // won't compile! // you cannot add anything, and only extract Object instances } private void foo(List list) { Object o1 = list.

Get(0); // ok TheClass o2 = list. Get(0); // ok list. Add(new Object()); // won't compile!

List. Add(new TheClass()); // won't compile! // You are sure that the objects are of a supertype of TheClass, // so you can extract TheClass instances safely.

However, you cannot // add anything to this list since its type is not known (may be // different from TheClass, so the compiler does not allow anything). } private void foo(List list) { Object o1 = list. Get(0); // ok TheClass o2 = list.

Get(0); // won't compile! List. Add(new Object()); // won't compile!

List. Add(new TheClass()); // ok // You are sure that the objects are of a type implemented by TheClass, // so you can add any TheClass instances to the list. However, you cannot // extract TheClass objects since the objects type may be just implemented // by TheClass, but different.

}.

I don't understand what the statement in my book means, can you explain it in the same context – rubixibuc Mar 20 at 8:12 I added code snippets to let you understand. – Aurélien Ribon Mar 20 at 8:18 In the sentence, what do "methods of type Object mean"? That's where I am confused.

Does it mean methods of the Object class? – rubixibuc Mar 20 at 8:21 The statement means that if there is no upper bound for wildcard, then upperbound is Object class (the ancestor for every class in Java). And compiler may only garantee that methods of upper bound (here Object) will be present in the class wich specified by this generic.

– yggdraa Mar 20 at 8:24 For me, "a method of type XXX which can be invoked on a value" is a strange way to mean "a method taking a XXX instance as parameter", or "a method invoked with XXX value as parameter". – Aurélien Ribon Mar 20 at 8:26.

Normal generic; ArrayList list= new ArrayList(); list. Put(man); Man m = list. Get(0); Using wildcard with upper bound: ArrayList list= new ArrayList(); list.

Put(man); //Man is a person.. Person p = list. Get(0); Using wildcard: ArrayList list= new ArrayList(); list. Put(man); Object o = list.

Get(0); If you use the wildcard you cannot know the generic type of ArrayList, hence you can only get Object out of the list which means that you are back to using ArrayLists the old way without generics..

That what I thought that everything is assumed to be the bound specified, but when I tested it I was able to extract not just Objects but whatever the upper bound was (of the generic not the referene). Of let's say the generic class after type erasure. So if I use MyClass and MyClass header equals MyClass will my variable of MyClass be able to return SomeOtherClass if that makes sense and only that class.

So would it be in that case that I can extract more than just objects of class Object – rubixibuc Mar 20 at 8:00 1 No, your second example won't compile, since your list type may be Woman, which extends Person but is not related to Man. As the compiler can't know, it won't compile on line 2 (when using the .put() method). – Aurélien Ribon Mar 20 at 8:04 The solution is to use super instead of extends in this second example.

Therefore, the list type can be any type implemented by Person, so a Man object will fit it, and the compiler will accept it. That's the PECS paradigm: when you want to consume objects (ie. Adding them to a list), you need super, not extends ;) – Aurélien Ribon Mar 20 at 8:06 The example was very contrived, but if the upper bound for a generic is a specific class, then when you use unbounded wildcard can you only extract objects of that upper bound, or only Object no matter what – rubixibuc Mar 20 at 8:07 If you don't bound the list, you can only extract Objects.

However, note that if you set a lower bound (using super), you can also only extract Objects. There are many tutorials and manuals about generics bounds, look for them, that's very instructive :) – Aurélien Ribon Mar 20 at 8:13.

For example you have wildcard. This means that any class matching this wildcard may be of MyClass type or any of it's descendants. But since the only thing we know it is descendant of MyClass, we can only garantee that methods available on class MyClass are available to invoke.

For wildcard with bo upper bound the upper bound will be Object class. And this means that all we know is that the class extends Object, so only methods that are garanteed to be present in this class are those that defined in Object class.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions