Trying to implement a generic and inheritance classes in a Java Project. For that reason I created the "Base" classes and Interfaces that will inheritance all the specific classes and interfaces.
I mean, I have a Base Interface
public interface BaseServices<T> {
public boolean validate(T item, int id) throws Exception;
}
And a Base Implementation for that Interface
public class BaseServicesImpl implements BaseServices<BaseBO> {
@Autowired
BaseDao dao;
CategoryBO bo;
public BaseServicesImpl()
{
this.bo = new CategoryBO();
}
@Override
public boolean validate(BaseBO item, int id) throws Exception {
return dao.validate("name",item.toString(), id);
}
}
Where BaseBO is the business object that ALL the other objects will extend.
And then, for an specific object, I will have a particular Interface which extends the Base one.
public interface CategoryServices extends BaseServices {
}
And its implementation:
public class CategoryServicesImpl extends BaseServicesImpl implements CategoryServices<CategoryBO> {
@Autowired
CategoryDao categoryDao;
public CategoryServicesImpl()
{
this.dao = categoryDao;
}
@Override
public boolean validate(CategoryBO item, int id) throws Exception {
return dao.validate("name",item.getName(), id);
}
}
Where some object can implement the generic "Validate" method (which validates just the name) or extend it to what the class requiredpublic class CategoryServicesImpl extends BaseServicesImpl implements CategoryServices.
How can I make this work? Is it possible? I'm trying to do the most I can to reuse my code. Let me know if there is another way.
EDITED: What I'm trying to do, is to have a base class with all the common method for all the classes, let say, the basic for create, edit, delete, get, getAll, etc. And then in each class I also can have and override for those method because in some classes they will be different, but in most, they will just do the same so it will call the generic ones that were defined in Base class.
BaseDao, and possiblyCategoryDao, should also be typed, probably with the same type as the implCategoryServicesImpl extends BaseServicesImpl, but why? I see no benefit from it.