File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Generics and Parent - child hierarchy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generics and Parent - child hierarchy " Watch "Generics and Parent - child hierarchy " New topic
Author

Generics and Parent - child hierarchy

Bharat Makwana
Ranch Hand

Joined: May 21, 2007
Posts: 107
hi guys,

First of all any help is highly appreciated.

I am writing one code using generic. I have one super class and two child class of said class.
I have one method which returns List of child(either list of first child objects or list of second child objects) object depending on some conditions.
Once I have this list, I also want to add one list(Child list) to this list.

right now code looks like :

class MainClass{

List<? extends Parent> pList;
List<Child> cList();

someMethod(){
pList = getParentList(); ( this method returns list of child objects depending of some conditions ,return type of getParentList method is List<? extends Parent>, and I can not change it)
cList = getChildList(); ( list of child objects)
cList.addAll(pList); // compile time error......


}


}

class Parent{}
class Child1{}
class Child2{}


Thanks




ॐ सर्वे जना: सुखिनो भवन्तु , तथास्तु |
'May the whole world be happy, so be it'

SCJP1.5, SCWCD1.5
John Bengler
Ranch Hand

Joined: Feb 12, 2009
Posts: 133
Hi Bharat,

I think the problem is that you add Objects of type "? extends Parent" to a List which only accepts Objects of type "Child". And "? extends Parent" can be something else.

Is it possible to change the type of your cList also to List<? extends Parent>?

If not I think you have to iterate through your parentsList, cast each element and add it to your cList..



John
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

Is it possible to change the type of your cList also to List<? extends Parent>?


That won't work either. You are correct that you can't add an Unknown type that extends parent element to a list that takes child elements, because there is no way for the compiler to confirm if the element type is correct.

With the change that you suggested, you will be trying to add an unknown type that extends parent element into a list that takes a specific unknown type that extends elements. There is no way for the compiler to confirm if the two unknown types are the some type.

Keep in mind, <? extends Parent> doesn't mean that it will support any type that extends parent. It means that it is a specific type that extends parent, and the compiler doesn't know what it is.

If not I think you have to iterate through your parentsList, cast each element and add it to your cList..


The purpose of generics is so that you don't have to cast. If you do, why bother using generics? You are overriding the type checks anyway, so why bother having the compiler check. If you want a list that can hold any unknown type that extends parent, you need a list that holds a parent type -- as any child type of parent IS-A parent.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Bharat Makwana
Ranch Hand

Joined: May 21, 2007
Posts: 107
Thank you very much for your time and replies.

Is it ok to type can child list to parent list? When i use type cast compiler does not sown error.

thanks
bharat
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

Bharat Makwana wrote:
Is it ok to type can child list to parent list? When i use type cast compiler does not sown error.


Basically, with generics, if the cast is okay, then you should not need to type cast it -- the compiler should allow it implicitly. If you have to type cast it, then you are telling the compiler that it is wrong, and frankly... as I said, if you need to do this frequently, then you shouldn't be using generics.

The reason type casting a child list to a parent list is a bad idea is that the parent list can hold any type of parent. And even though a child IS-A parent, not all parent types are childs. So, you will break any child references to that list.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics and Parent - child hierarchy