when answering the question Creating generic array in Java via unchecked type-cast, newacct said
(The lower bound of Bar is Object in this question. In a case where the lower bound of Bar is something else, replace all occurrences of Object in this discussion with whatever that bound is.)
Here is newacct's code:
class Foo<Bar> {
Bar[] bars = (Bar[])new Object[5];
public Bar get(int i) {
return bars[i];
}
public void set(int i, Bar x) {
bars[i] = x;
}
public Bar[] getArray() {
return bars;
}
}
I wonder whether Object
is the upper bound or lower bound of Bar
. I think that Foo<Bar>
is short for Foo<Bar extends Object>
, so Object should be the upper bound of Bar
, am I wrong?
You can define the lower bound as follows in generics as well.
}
So you can create a Foo object with any class that extends Bar.
No, you cannot use Object typed instances and lower bound is Bar and upper bound is any class that extends Bar class.