Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mike Cheung
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


It works as above.

But it wont work as below



you see compilation error saying mismatch.
 
Henry Wong
author
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: why did you say “array” in the thread subject?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic