I have the following classes:
abstract class Parent
{
private ClassDependentOnSize classDependentOnSize;
public Parent ()
{
this.classDependentOnSize = new ClassDependentOnSize(size());
}
public abstract int size ();
}
class Child extends Parent
{
private String DBSelection;
private String[] DBSelectionArgs;
public Child (String selection, String... selectionArgs)
{
super();
this.DBSelection = selection;
this.DBSelectionArgs = selectionArgs;
}
@Override
public int size()
{
//FAILS because Parent calls size before DBSelectionArgs is initialized
String temp = "";
for(String str : DBSelectionArgs)
temp += str;
return temp.length;
//This function basically does a calculation that should not be
//done before hand. I have posted the exact method below.
}
}
class ClassDependentOnSize
{
public ClassDependentOnSize(int size)
{
}
}
While this are not my exact class, this is the same problem. I have written this one to remove all the unnecessary code.
As you can see, the super class tries to call size () before the child is finished initializing to construct the class dependent on size. I am curious on how anyone has resolved this in the past. As I am sure everyone is aware, super() must be the first line in a child constructor. As I have laid these classes out, is this a bad design? I feel like this is a problem that must have happened before, but I can not find a solution.
EDIT: Here is the exact method. It uses for an Android application.
protected Cursor getCursor()
{
if (songCursor == null)
{
songCursor = GET_SONGS_CURSOR(getContext(), MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.toString(), selection, selectionArgs, selection);
if (!songCursor.moveToFirst())
Standard.Loge("|NewPlaylist.getCursor| Could not move to first.");
}
int count = songCursor.getCount();
songCursor.close();
return count;
}