Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java cannot access top class property in subclass's redefined method

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 ?

like image 203
Sterling Duchess Avatar asked Dec 20 '12 14:12

Sterling Duchess


2 Answers

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.

like image 115
Patricia Shanahan Avatar answered Oct 25 '22 19:10

Patricia Shanahan


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.

like image 33
lpinto.eu Avatar answered Oct 25 '22 17:10

lpinto.eu