I'm getting a warning when calling collections.sort() when passing an ArrayList
containing instances of subclasses of an abstract class.
Code:import java.lang.Comparable; public abstract class Person<V extends Comparable> implements Comparable<V> { public String name = ""; public int age = 0; public Person() { } public int compareTo(V o) { return this.age - o.age; } } // PersonCode:public class Drunk extends Person { public double BAC = 0; public Drunk() { super(); } } // DrunkCode:import java.lang.Comparable; import java.util.ArrayList; import java.util.Collections; public class GenericsTest { public static void main(String[] args) { ArrayList<Person> ppl = new ArrayList<Person>(); Drunk myDrunk1 = new Drunk(); Drunk myDrunk2 = new Drunk(); ppl.add(myDrunk1); ppl.add(myDrunk2); Collections.sort(ppl); } } // GenericsTest
Produces the warning: warning: [unchecked] unchecked method invocation: <T>sort(java.util.List<T>) in java.util.Collections is applied to (java.util.ArrayList<Person>)
Collections.sort(ppl);
^
I think there is something wrong with the way I'm declaring the abstract class Person.





Reply With Quote