• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Generics Doubt.

 
Ranch Hand
Posts: 463
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI All,

Here is my programme.

import java.util.ArrayList;
class Master { }
class Parent extends Master { }
class Child extends Parent { }

public class GenericTest {
public ArrayList add(ArrayList<? super Parent> aList) {
aList.add(new Master());
return aList;
}

public static void main(String[] args) {
GenericTest g = new GenericTest();
System.out.println(g.add(new ArrayList<Parent>()));
}
}

This programme failed to compile. Because of add(...) method. As per its signature it should accept any arraylist of type Parent or its super classes. Howerver, if I use Child while adding the element to list the code working fine.

Can anyone please explain me why this is behaving like this.

? super AClass - Does this means that any super class of AClass?
? extends AClass - Does this means that any sub class of AClass?

Thanks in advance,
Surya.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,
Sai Surya posted November 13, 2006 04:27 AM

? super AClass - Does this means that any super class of AClass?
? extends AClass - Does this means that any sub class of AClass?


yes, both of them. And including AClass itself (also for both of them).

There is another major difference between super and extends here.
? extends AClass only allows only to read in the collection. Trying to change (generic) values in the collection - for example add - will cause this funny capture of ? compiler error. I must be so, because you cannot add parents to a child list.

? super AClass does allow to change values or add things. This is because the type is at least AClass, you are allowed to add AClass objects to it (eg, the list can be <Parent> or <Master> in your example.

But you can only add objects that are of the type specified (or lower in hierarchy).

So in your example:


you can add parents and childs to "aList". Adding a master will not compile.

If you want a method that takes ArrayLists of Masters and subclasses, you could write


Then you could use this for ArrayLists of Master and subclasses and put the appropriate objects in.
But even calling the method right, e.g.
ArrayList<Master> masters = new ArrayList<Master>();
masters = g.add(masters , new Child() );

will cause a compiler warning (yet it compiles).

ArrayList<Parent> parents = new ArrayList<Parent>();
g.add(parents, new Master()); // no
however would not compile.

So we have our typesafety here.


I find it a bit tricky, all this generics stuff...


Yours,
Bu.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote

you can add parents and childs to "aList". Adding a master will not compile.




I forgot to say why: Because you COULD invoke your <? super Parent>-method with an ArrayList<Parent>. What you did by the way.
And for type safety, it cannot compile if you add a master to a parents list.

Bu.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If you insert the above code in the class GenericTest it compiles fine.
As per "Burkhard Hassel" it should not compile, since we are changing the collection.

but if you change code at // line 2 above as
aList.set (1, new Master ());
(or)
aList.set (1, new Parent ());
(or)
aList.set (1, new Child ());

compilation fails.
So, what is the exact rule here.
waiting for reply...
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers!

Naresh Devasani posted November 13, 2006 07:13 AM






If you insert the above code in the class GenericTest it compiles fine.



Yes, that's what I forgot. You cannot add (set ...) anything.
Except null. Because you can cast null to any type.

and:

So, what is the exact rule here.
waiting for reply...


Perhaps there is something in the language specification. I don't care about the exact rule.

Not really exact but short rule:
<? extends A_class>
you cannot add anything (except null).

<? super A_class>
you can add anything of type A_class and subtypes. You may also add null.




Yours,
Bu.
 
Sai Surya
Ranch Hand
Posts: 463
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Burkhard,

Thanks for the great replies, I may have to spend a couple of hours to understand this. This looks tricky.

Thanks & Regards,
Surya.
 
Get off me! Here, read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic