youngrok
youngrok

Reputation: 132

instantiate subclass in superclass static method

I'm developing ORM in Java. I want to do this.

class Model {
    public static Model find(Object id) {
    }
}

class User extends Model;
class Post extends Model;

User user = User.find(15);
Post post = Post.find(1325);

How can I do this, or similar things. I don't want to pass Class object to find method.

Upvotes: 0

Views: 584

Answers (1)

duffymo
duffymo

Reputation: 308948

You should not do this. A super class can never know how it's extended.

Use generics instead:

package persistence;

public interface GenericDao<T, K extends Serializable>
{
    T find(K id);
    K save(T value);
    void update(T value);
    void delete(T value);
}

Upvotes: 2

Related Questions