aspose file tools*
The moose likes Java in General and the fly likes Why can't I pass an array of a specific type to a function that accepts an array of Object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why can Watch "Why can New topic
Author

Why can't I pass an array of a specific type to a function that accepts an array of Object?

Mike Cheung
Ranch Hand

Joined: Feb 01, 2013
Posts: 88
Hi, question on why I can't pass objects of a specific type into another function, if the objects are in a list. Here are my code snippets.

Given the following class for persisting objects into database ....



I'm using the following main class to create the objects to be saved into database ....



Notice in the "Persisting single animal" section, I'm passing over an object of the Animal type into the function which takes a variable of Object type? This works.

Notice in the "Persisting a list of animals" section, I'm passing a List object of the Object type into the function which takes a variable of Object type? This works also. But the reason why I'm passing a List<Object> rather than a List<Animals> is because if I use List<Animals> I'll get the following error.

error: incompatible types: List<Animals> cannot be converted to List<Object>
This is being reported on the following line.


So why it works for a single object but not for a List of objects?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Mike Cheung wrote:Hi, question on why I can't pass objects of a specific type into another function, if the objects are in a list. Here are my code snippets.

[DELETED -- not needed for this response]

error: incompatible types: List<Animals> cannot be converted to List<Object>
This is being reported on the following line.


So why it works for a single object but not for a List of objects?


Just because an Animal IS-A Object, and hence, can be used in place of an Object, doesn't mean that the relationship extends to List<Animal> and List<Object>. There are tasks that you can do with List<Object> that can't be done with List<Animal>. And there are tasks that can be done with List<Animal> that can't be done with List<Object>. In other words, the inheritance hierarchy of the elements doesn't apply to their generic containers.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


BTW, given what you are trying to do...

Mike Cheung wrote:
Given the following class for persisting objects into database ....



To persist a list of items, and you don't really care what the generic type is... perhaps you meant to use wildcards, instead of using the top base class?

In other words, perhaps you want the parameter to be a List<?> and not a List<Object>?

Henry
Mike Cheung
Ranch Hand

Joined: Feb 01, 2013
Posts: 88
Just because an Animal IS-A Object, and hence, can be used in place of an Object, doesn't mean that the relationship extends to List<Animal> and List<Object>. There are tasks that you can do with List<Object> that can't be done with List<Animal>. And there are tasks that can be done with List<Animal> that can't be done with List<Object>. In other words, the inheritance hierarchy of the elements doesn't apply to their generic containers.

Um... I get your drift but I'd have thought the operations will be on the collections, which is List in this case, and the actual operations for Animal wouldn't be available unless you are working on the elements of the List itself.

To persist a list of items, and you don't really care what the generic type is... perhaps you meant to use wildcards, instead of using the top base class?

In other words, perhaps you want the parameter to be a List<?> and not a List<Object>?

Okay thanks for the advice.
sai rama krishna
Ranch Hand

Joined: May 29, 2009
Posts: 269


It works as above.

But it wont work as below



you see compilation error saying mismatch.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Mike Cheung wrote:
Just because an Animal IS-A Object, and hence, can be used in place of an Object, doesn't mean that the relationship extends to List<Animal> and List<Object>. There are tasks that you can do with List<Object> that can't be done with List<Animal>. And there are tasks that can be done with List<Animal> that can't be done with List<Object>. In other words, the inheritance hierarchy of the elements doesn't apply to their generic containers.

Um... I get your drift but I'd have thought the operations will be on the collections, which is List in this case, and the actual operations for Animal wouldn't be available unless you are working on the elements of the List itself.


Okay, you have a List<Object> and a List<Animal>, are the List operations truly interchangeable?

With a List<Object>, I can add Tables, Chairs, Cars, Houses, in addition to Cats and Dogs. With List<Animal>, I can't do that.

With a List<Animal>, I can iterate through it, and know that everything in it IS-A Animal. No need to type check, or cast to Animal. With List<Object>, I can't do that.


Given these differences, which assignment should be allowed? Should a List<Animal> be allowed as a List<Object> parameter? Should a List<Object> be allowed as a List<Animal> parameter?

Henry
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
By the way: why did you say “array” in the thread subject?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Why can't I pass an array of a specific type to a function that accepts an array of Object?