So I’m having a bit of an issue getting my properties to set properly in a super class type scenario.
I have 2 classes such that class B is a specialized version of class A, let’s say…
public class A {
private String name;
private int id;
...
}
public class B extends A {
private Date time;
private int status;
...
}
Now what I’m trying to do is use a method that is used to set properties in A from a result set, but instead set them in an instance of B.
public A setProperties(ResultSet rs) {
A a = new A();
a.setName(rs.getString(...));
...
return a;
}
I have tried taking the return from this and casting it as a B, but of course not all A’s are B’s… so that doesn’t work. I have also tried adding another parameter to the setProperties method so that it takes in an A and returns an A, that way I can use polymorphism to get my B back, but then all my values were nulled out.
I’m at a loss here, any recommendations are greatly appreciated.
Declare the method in the superclass. The subclass will override this method and then invoke
super.foo(ResultSet rs), wherefoo(...)is the overridden method. Therein, you can parse theResultSetand set the object’s fields.Example –
For more information, see Using the Keyword super.