inheritance - Java: Using one Method for different types in the array parameter -


i cant't figure out maybe can me.

the problem:


public class class1 implements comparable<class1> {     public int compareto(class1 o) {         //some code     } }  public class class2 implements comparable<class2> {     public int compareto(class2 o) {         //some code     } }  public class foo {     private arraylist<class1> abc = new arraylist<class1>();      private arraylist<class2> def = new arraylist<class2>(); } 

in following o = abc or def

public int foo1 (arraylist<object> o) {    o[0].compareto(o[1]); } 

this code keeps getting me error:

the method compareto(class1) undefined type object 

i understand why can't find workarround don't have duplicate code, necessary because have more arraylist objects , longer code.

i hope 1 of has idea.

problem solved!!! solution :

public <t extends comparable<t>> int foo1 (arraylist<t> o) {   return o.get(0).compareto(o.get(1)); } 

by peter lawrey

when use object it's super class methods, cannot assume it's subclass methods available.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -