Android : Implement sort of "static abstract and inheritable" method

on Monday, October 6, 2014


I am working on an Android App, I got this situation:


-GenericOrderDetailFragment, an Abstract Class that extends Fragment.


-Three non-abstract "sons" of this class: LinesFragment, HeaderFragment, NoteFragment.


There are plenty of methods that are the same in the three sons, so i declare then on the abstract class, that is why it exists.


I'm stuck with one in particular. By now, I put this method inside the son and it works great:



public class LinesFragment extends GenericOrderDetailFragment {

[...]

public static LinesFragment newInstance(Order o) {
LinesFragment fragment = new LinesFragment();
putSerializable(o, fragment);
return fragment;
}

[...]

}


This method is identical in all the sons, except for the return type: LinesFragment should return a LinesFragment instance, HeaderFragment should return a HeaderFragment istance ... I wanna put this as a mustoverride in the father class, but I cannot do this because of:


Simply put the



public static GenericOrderDetailFragment newInstance(Order o) {
GenericOrderDetailFragment fragment = new GenericOrderDetailFragment();
putSerializable(o, fragment);
return fragment;
}


method does not work, simply because I cannot istantiate a GenericOrderDetailFragment (It is abstract). I cannot istantiate a this .getClass() item, 'cause there is not this : the method is static!


I cannot declare a "static abstract" method (in java static is "not overridable" and abstract is "mustoverride", so they cannot live together), so I cannot force the user who extends GenericOrderDetailFragment to override the method.


There are methods I MUSToverride if I extend this Generic Class, so I cannot declare not-abstract the father class.


Is ther any workaround I can use? Thanks in advance!


0 comments:

Post a Comment