What is an heterogeneous object in java?
Asked Answered
U

3

17

When I am using any collection of generic type with Object class and I'm storing different objects in that collection in that situation.

Can I say that the collection contains heterogeneous objects or not?

Underprivileged answered 19/9, 2018 at 6:48 Comment(0)
A
18

I've heard this term only once when a heterogeneous object referred to a heterogeneous collection, a collection that can store objects of different types.

A heterogeneous collection could be an Object[] array, or List<Object> list. We rarely use them (their declarations are too general - "a collection of everything" indicates design issues), but it's clear that it can contain instances of different types (e.g. Integer and String).

Advert answered 19/9, 2018 at 6:56 Comment(0)
E
5

"homo-" means same, "hetero-" means different.

In any case if a single Java array can only store one type, say, only numbers, or only strings then it is homogeneous.

If multiple types then heterogeneous. In the above case, since collection is of Object type and can hold any type

Let's take Javascript for understanding this better eg. arrays are heterogeneous, because I can make an array that holds [5, "hello", new Object()], and in Java that's not possible.

Endophyte answered 3/4, 2020 at 3:9 Comment(0)
E
3

I don't think that heterogeneous objects is a commonly used term that comes witha a clear and well defined meaning.

Thus your definition that says: objects of different classes are heterogeneous is fine.

( The problem arises later, when you assume that other people share that same understanding, and that is not necessarily true )

Excogitate answered 19/9, 2018 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.