Forums Register Login

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

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send

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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send


It works as above.

But it wont work as below



you see compilation error saying mismatch.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
By the way: why did you say “array” in the thread subject?
Slime does not pay. Always keep your tiny ad dry.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 731 times.
Similar Threads
Polymorphism and Generics - ideas taken from SCJP 6 by K and B
Generics problem- why not taking Animal, Object and super of Dog
generic question
generics doubt
about reference type
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 15:32:56.