At the moment my class looks something like this (very simplified):
I have three classes to describe either Nodes or Ways (from OpenStreetMap):
public abstract class Geometry {
private String id;
public Geometry(String id) {
this.id = id;
}
}
public class Node extends Geometry {
private GeoPoint location;
public Point(String id, GeoPoint location) {
super(id);
this.location = location;
}
public GeoPoint getLocation() {
return location;
}
}
public class Ways extends Geometry {
private ArrayList <GeoPoint> shape;
public Point(String id, ArrayList <GeoPoint> shape) {
super(id);
this.shape = shape;
}
public GeoPoint getShape() {
return shape;
}
}
Now I want to iterate through an ArrayList with the class Geometry and use methods from both subclasses:
private void prepareList(ArrayList<Geometry> geometries) {
for (Geometry m : geometries) {
if (m.getClass().equals(Node.class)) {
location = m.getLocation();
}
else if (m.getClass().equals(Way.class)) {
shape = m.getShape();
}
}
}
In my solution, I need to make some dummy methods in the class Geometry to access these methods, like public GeoPoint getLocation() { return null; }
My question now is, what is the best way to implement this in Java, without making separate classes (which leads to code duplicates) or writing this "dummy" methods. Is there a better way?