Ok, so I here is the structure of the relevant parts of my program. I have an interface ProductSQL and two classes which implement this interface. Both are called ProductSQLImpl and they reside in different packages. One implementation extends the other implementation and only contains a couple of methods that override the parents. So to summarize, I have one implementation with all the methods defined in the interface, and a subclass of that implementation with only a few overriding methods (differences in SQL statements).
The reason for this structure is because I have to two apps, both very similar except that one is stripped down version of the other. It has most of the same structure except that its SQL statements differ slightly. So the DAO class which builds the data objects must have different SQL statements for each app, but the DAO classes themselves can remain the same.
I am using the Spring framework container to inject the appropriate SQL implementation into the DAO class. My problem arises at run time. I have placed a breakpoint inside one of the DAO methods. This method should call one of the overridden ProductSQLImpl (the subclass version) methods, which builds and returns a query. However, execution goes to the parents implementation of that method, even though the object is that of the subclass.
Why is that?
Below is some of the basic relevant code:
public interface ProductSQL {
public String query(Params...);
...
}
public class ProductSQLImpl implements ProductSQL {
public String query(Params...) {
String sql;
...
return sql;
}
}
public class ProductSQLImpl extends ProductSQLImpl {
//overridden
public String query(Params...) {
String sql;
...
return sql;
}
}
public class ProductDAO {
private ProductSQL sql;
// subclass implementation is injected from by spring container
public void setProductSQL(ProductSQL sql) {
this.sql = sql;
}
...
}