misunderstanding about generic

see those codes

public class C1{

// do something

}

public class C2 extends C1{

// do something

}

public class TestClass{

public void FuncA(ArrayList<C1>){

// do something

}

public static void main(String args[]){

TestClass clazz = new TestClass();

ArrayList<C2> aList = new ArrayList<C2>();

// do something

clazz.FuncA(aList); // < attention please!

}

}

It's just a simple use of Generic feature.I define a Function with a parameter in type ArrayList, and I also take the generic feature into the lines.

But just at compile time, IDE tells me that:

The method FuncA(ArrayList<C1>) in the type TestClass is not applicable for the arguments (ArrayList<C2>)

I know the Java language may treate this feature strictly, but in my way, it also performs a safely use!Because C2 is derived from C1,I use a sub-type as parameter to pass into function which is defined with a parameter in parent-type, it's very safy...

How should I consider this...

[1134 byte] By [Eliottea] at [2007-11-27 7:58:39]
# 1
The sub calss will contain more features than its super class.So I think it should only work the other way, that is if C1 is derived from C2.Achyuth
achyuthba at 2007-7-12 19:40:36 > top of Java-index,Java Essentials,Java Programming...
# 2

It's not safe, cause what you're saying in your code is the following:

1.) aList is a list that's guaranteed to contain instances of C2

2.) FuncA is a method that works with lists which are guaranteed to contain instances of C1

So far, so good. Unfortunately, FuncA might add a C1 to its list parameter; this way it would break 1.)

quittea at 2007-7-12 19:40:36 > top of Java-index,Java Essentials,Java Programming...
# 3

So,as you said,generic must be very strict, and no inheriting supported?

i.e.

FuncA(ArrayList<C1> aList){

for(C1 clazz : aList){

// do something

}

}

When I use objects contained in aList in method FuncA,I don't know that the object clazz either refer to type C1 or type C2.So I could only use it as an instance of type C1.

May be I misunderstand...

Anyway,thanks all of u.

T_T ... my poor English. I come from a non-English speaking country.

Eliottea at 2007-7-12 19:40:36 > top of Java-index,Java Essentials,Java Programming...
# 4

This works!!.

import java.util.*;

class C2{

//do something

}

public class C1 extends C2{

//do something

}

class TestClass{

public void FuncA(ArrayList<C1> list){

//do something

}

public static void main(String args[]){

TestClass clazz = new TestClass();

ArrayList<C1> aList = new ArrayList<C1>();

//do something

clazz.FuncA(aList); // < attention please!

}

}

achyuthba at 2007-7-12 19:40:36 > top of Java-index,Java Essentials,Java Programming...
# 5

> So,as you said,generic must be very strict, and no

> inheriting supported?

Oh yes, there is. Change the signature of FuncA to this:

public void FuncA(List<? extends C1> aList)

quittea at 2007-7-12 19:40:36 > top of Java-index,Java Essentials,Java Programming...
# 6
> This works!!.Unfortunately, it's not what the OP wants.
quittea at 2007-7-12 19:40:36 > top of Java-index,Java Essentials,Java Programming...