I have the following in my top class working like a charm:
public class TestA<E extends Test>
{
List<Vector<E>> list;
String name, location;
TestA(String name, String location)
{
this.name = name;
this.location = location;
list = new ArrayList<Vector<E>>();
}
void populate(Test[] arr)
{
for(Vector<E> vk : list)
{
// Code here...
}
}
}
class OrderedTest<E extends Test> extends TestA {
OrderedTest(String name, String location)
{
super(name, location);
}
void populate(Test[] arr)
{
for(Vector<E> vk : list) // ERROR: Cannot convert from element Object to Vector<E>
{
// Code here...
}
}
}
When i try to extend populate() in my subclass i basically want the same method only here i want things ordered so i will pull each Vector in a quicksort method but i dont get any other problems apart from: // ERROR: Cannot convert from element Object to
EDIT: Should i have to implement Iterator for TestA ?
As coded, the two uses of "E" as a generic parameter are independent and disconnected. Vector<E>
in one class does not mean the same as Vector<E>
in the other.
Change the subclass declaration to:
class OrderedTest<E extends Test> extends TestA<E>
and the E in the superclass declarations is the same E as in OrderedTest.
You declared a Generic
class TestA<E>
defining a field of type List< Vector< E > >, means that the vector will contain elements of type E, that also mean that if you don't define E, when instantiation of subclassing TestA the Vector will be Vector.
The child class also a generic is defined as:
class OrderedTest<E extends Test> extends TestA
defining this generic reusing the letter 'E' doesn't bind it to the father class. That said you didn't define father class E type, and that why it is Object (the default type).
If you want to define the father class generic type to be the same as the child class you must define it as: to
class OrderedTest<E extends Test> extends TestA<E>
note that the letter 'E' here doesn't have any connection with the father 'E' parameter, and can be any letter since where it is a parameter for class OrderedTest, you just decided to use this type parameter to be the one defining the father type parameter also. So you can do for instance:
class OrderedTest<T extends Test> extends TestA<T>
The important thing is to define the father class type property with the one that will be defined to the child, so you can access the list field expecting the same data types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With