Generics question

J

Joona I Palaste

The generics introduced in Java 1.5 are quite an interesting concept,
when it comes to inheritance. I therefore have a question:
Suppose both SuperList and SubList are template-able (or what is the
correct term?) collections and SubList is a subclass of SuperList.
Further suppose SubElement is a subclass of SuperElement but these don't
have anything to do (inheritance-wise) with the two list classes above.
Is SubList<SubElement> now a subtype of both SuperList<SubElement> and
SubList<SuperElement>? Is it a subtype of SuperList<SuperElement>?
What about SubList<SuperElement> and SuperList<SubElement>, is either a
subtype of the other?
In general, if FoobarList is a template-able collection, is
FoobarList<A> a subtype of FoobarList, no matter what class A is?
 
T

Tony Morris

Joona I Palaste said:
The generics introduced in Java 1.5 are quite an interesting concept,
when it comes to inheritance. I therefore have a question:
Suppose both SuperList and SubList are template-able (or what is the
correct term?)

There isn't a correct term, but to assimilate generics with templates will
lead to confusion in the long run.
collections and SubList is a subclass of SuperList.
Further suppose SubElement is a subclass of SuperElement but these don't
have anything to do (inheritance-wise) with the two list classes above.
Is SubList<SubElement> now a subtype of both SuperList<SubElement>
no

and
SubList<SuperElement>? Is it a subtype of SuperList<SuperElement>?
no

What about SubList<SuperElement> and SuperList<SubElement>, is either a
subtype of the other?
no

In general, if FoobarList is a template-able collection, is
FoobarList<A> a subtype of FoobarList, no matter what class A is?
no


--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"O pointy birds, O pointy-pointy. Anoint my head, anointy-nointy."
- Dr. Michael Hfuhruhurr


It's all explained here:
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
 
O

Oscar kind

Joona I Palaste said:
The generics introduced in Java 1.5 are quite an interesting concept,
when it comes to inheritance. I therefore have a question:
Suppose both SuperList and SubList are template-able (or what is the
correct term?) collections and SubList is a subclass of SuperList.
Further suppose SubElement is a subclass of SuperElement but these don't
have anything to do (inheritance-wise) with the two list classes above.
Is SubList<SubElement> now a subtype of both SuperList<SubElement> and
SubList<SuperElement>?

No. Just of SuperList<SubElement>. See section 3 of Sun's tutorial for an
explanation: http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

Is it a subtype of SuperList<SuperElement>?

No. This was asked above.

What about SubList<SuperElement> and SuperList<SubElement>, is either a
subtype of the other?

No. Same reason as above.

In general, if FoobarList is a template-able collection, is
FoobarList<A> a subtype of FoobarList, no matter what class A is?

No. Same reason.
 
O

Oscar kind

Tony Morris said:

I beg to differ.

My reasoning is as follows: SubList is a subtype of SuperList, and both
SubList<SubElement> and SuperList<SubElement> pose the same restrictions
on the elements. Thus, SubList<SubElement> is a subtype of
SuperList<SubElement>.

I believe this is also why the following line, taken from a program of
mine, compiles:
List<Long> tickerIds = new ArrayList<Long>();
 
J

Joona I Palaste

Oscar kind said:
I beg to differ.
My reasoning is as follows: SubList is a subtype of SuperList, and both
SubList<SubElement> and SuperList<SubElement> pose the same restrictions
on the elements. Thus, SubList<SubElement> is a subtype of
SuperList<SubElement>.
I believe this is also why the following line, taken from a program of
mine, compiles:
List<Long> tickerIds = new ArrayList<Long>();

Note the word "both". The question continued: "...and
SubList<SuperElement>". AFAIK type inheritance in generics works only in
the surrounding container type, not the enclosed element type.
 
M

Mike Schilling

Joona I Palaste said:
Note the word "both". The question continued: "...and
SubList<SuperElement>". AFAIK type inheritance in generics works only in
the surrounding container type, not the enclosed element type.

Consider the following:

List<String> slist = new ArrayList<String>();
List<Object> olist = slist; // This does not compile
olist.add(new Object()); // This would compile OK, but it's a bad
idea

From that point of view, it's good that List<Object> isn't assignable from
List<String>.

The same situation can arise with arrays, e.g.

String[] sarr = {"a", "b"};
Object[] oarr = sarr;
oarr[0] = new Object(); // compiles OK, but throws at run time

but an array does a special run-time check when a member is assigned. A
collection, in general, does not.
 
V

Vincent Cantin

The generics introduced in Java 1.5 are quite an interesting concept,
when it comes to inheritance. I therefore have a question:
Suppose both SuperList and SubList are template-able (or what is the
correct term?)

Let's say that they are "generically type-checkable" ... so there is no
longer any confusion with template or even containers.

Generics are not only for collections .. we can use it where we want to
check the type of something, and of course it is the case for the
collections.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

generics puzzle 57
Generics 24
Hairy generics question 29
Generics Amanuensis? 2
can this be done with generics? 32
Generics 12
Tree design with generics 2
Generics for a multiplevalue hashmap 8

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top