What is an heterogeneous object in java?

18.4k views Asked by At

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?

3

There are 3 answers

0
GhostCat On

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 )

0
Andrew Tobilko On

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).

0
Darshan On

"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.