• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic query - wildcards

 
Paul Stat
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following code



Why is it that you can't do this?

 
Deepak Giri
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as far as i know, you can't do that because you are initializing a List of type <B>
So how could you add an <A> type object in that list? (because A is not B)



you're declaring that the List can be initialized as type B or any of its (direct or indirect) superclasses.
But then, you are initializing the List as a type B


(to the experienced ranchers here: i guess i am thinking right, if not please correct me (apologies))
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because all the compiler know is that list03 is a list of objects of class B or its superclasses, but it doesn't know which exactly. It may actually be a list of Objects, or a list of A, or a list of B. If it is a list of B (as in your example), when you try to add an A (what you are doing) or an Object, it is an error and things would blow up. What would happen if you wrote this:
A a = new A();
B b = a;
You must know already that you cannot assign a superclass to a reference to a subclass without casting. So, that's why the compiler complains. If it was allowed, the next time you call list03.get(), and the object returned would be the A you added, it would be cast to B, and a cast exception would be on the way.
When you add new B(), the compiler knows that since the list is either a list of Object, A or B, it is safe to add any object of class B or its subclasses.
Hope this helps, I also had quite hard time when I started with generics.

Raf
 
Paul Stat
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right ok I think I understand now, infact here's some more examples



Although I'm not sure what use if any you could get from the above
 
Deepak Giri
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


now after every list you initialize (in the last code example), insert the different possible (which you think are possible) add() statements/methods and check if you can get it clearer!
 
Paul Stat
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Giri wrote:

now after every list you initialize (in the last code example), insert the different possible (which you think are possible) add() statements/methods and check if you can get it clearer!


Well assuming we're talking about the "? super" examples, you can still only add the type that is defined on the left, at least this is what the compiler told me when I was trying it out.
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, this (from Java API) (suppose A and B extends A):

1. extends - java.util.ArrayList has addAll() that takes a collection, and all it needs to do with the collection is to read it, and add every element to itself. The type of the collection's elements must be a subtype of the array list's generic type. This way the addAll method is very flexible, it can be passed a collection of subclasses of the array list's elements.

For example:


2. super - java.uti.Collections has a sort() method which can take an object that implements Comprarator, that can compare objects of a given type. However, if a comparator can compare objects of type A, it will also be able to compare objects of type B, as B is A (inheritance). You might have a comparator of A that will work in a given scenario, and you might not want / need to make a more specific comparator. So:

For example:

If the API did not use <T extends Comparable><? super T>> in the method signature, you would not be able to pass a comparator of A to sort a list of B.

So, extends and super, whereas not trivial, are really nice in some cases, and you have been probably using them not even knowing, and using their features. Hope this clears your doubts as for what extends and super can be used ;-)

Raf
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic