I'm creating generic DAO for my DataNucleus JDO DAOs. Generic DAO will do get, update, delete, create operations and some other generic operations, so those implementations can be just extended in more specific DAOs.
Is it possible to somehow extend generic DAO and have it return correct types when for example get object by id?
User user = userDao.get(userId); // Is this possible when UserDao extends generic DAO ?? userDao should return user of type User instead of object.
Yes, it is possible to do this with generics:
public abstract class Dao<T> {
public T get(String id) { ... }
...
}
public class UserDao extends Dao<User> {
...
}
UserDao userDao = new UserDao();
User user = userDao.get(userId); //Returns a User
Depending on your needs, Dao<T>
can be an abstract base class, or a generic interface (e.g. public interface IDao<T> { ... }