Reputation: 14077
I have 2 classes
In these classes I have an items ArrayList.
How can I call a method like:
...
if (i==1) method(classCurvePath);
if (i==2) method(classLinearPath);
..
void method(Object class) {
ArrayList al = class.items;
}
Do I need an interface? How can I do this? This is not working: "cannot be resolved or not a field"
Upvotes: 1
Views: 126
Reputation: 81154
Yes you probably want an interface (maybe even an abstract class):
public interface Path {
List<Item> getItems();
}
public class CurvePath implements Path {
public List<Item> getItems() {
//specific implementation for curved path, maybe
return items;
}
}
public class LinearPath implements Path {
public List<Item> getItems() {
//specific implementation for linear path
return items;
}
}
//...
void method(Path clazz) {
ArrayList<Item> al = clazz.getItems();
}
Upvotes: 3