generics - Java DAO factory dynamic returned object type -


i'm trying write simple dao create entity objects based on type stored in string field. how return type changed dynamicly? method findbyid() of userdao class should return user class object. same method productdao should return product. don't want implement findbyid in every class extends dao, should done automatically.

example code:

class dao {    protected string entityclass = "";    public (???) findbyid(int id) {       // db query       return (???)entityfromdatabase; // how this?    } } class userdao extends dao {    protected string entityclass = "user"; } class productdao extends dao {    protected string entityclass = "product"; } class user extends entity {    public int id;    public string name; } 

modify to

class dao<t> {    //   protected string entityclass = "";    public t findbyid(int id) {        return (t)entityfromdatabase; // how this?    } } class userdao extends dao<user> {    //protected string entityclass = "user"; } class productdao extends dao<product> {    //protected string entityclass = "product"; } class user extends entity {    public int id;    public string name; } 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -