Ok so I know that you can't have an abstract static method, although I see this as a limitation personally. I also know that overriding static methods is useless because when I am dealing with say MyList<T extends ObjectWithId>
and my object has an abstract class with a static method that gets overridden in it's subclasses, T doesn't exist at runtime so ObjectWithId
's static method would be called instead of the subclass.
So here is what I have:
class PersistentList<T extends ObjectWithId> implements List<T>{
}
where ObjectWithId
is:
abstract ObjectWithId{
public abstract long getId();
}
Now the issue is that my PersistentList is meant to be stored on hard disk, hence the name, and in reality will only store ids of objects it holds. Now when I want to implement the
@Override
public T get(int index) {
}
method of PersistentList
, what I want is for my program to use the id
it has stored for index
and call a static method objectForId(long id)
which would be implemented in each subclass of ObjectWithId
. It can't be a instance method because there is no instance yet, the point is to load the instance from the hard disk using the id
. So how should it be implemented? One option is to have ObjectWithId
have a constructor ObjectWithId(long id)
implemented in each subclass, but T doesn't exist at runtime so how would I instantiate it? I know I could pass Class<T>
object in the constructor of PersistentList
but I would prefer if the constructor did not have any arguments, but I don't think there is a way to get the class of T without explicitly passing it in right?
I hope this is a better explanation, sorry for the ambiguous question I started with.