Reputation: 91
UPDATE:
Thanks to @RC for solving this for me:
public class DataHandler<I, T extends IDataStore<I>> implements IHollywood<T, I>
From his comment I learned how to fix the ActorImpl:
public class ActorImpl extends DataHandler <Long, Actor> implements IHollywood
Solved.
I am currently trying to learn Java and am having some difficulty with generics. I am stuck on how to correctly implement a generic interface using two different type parameters and then extend this new implementation by two other interfaces that extend the original interface.
This is what I am trying to accomplish:
public interface IDataStore<ID> extends Serializable
public interface IHollywood<T extends IDataStore <ID>, ID>
{
List<T> retrieve() throws Exception;
void sotre(T t) throws Exception;
}
public interface IActor extends IHollywood <Actor, Long>
public interface IDirector extends IHollywood <Director, Long>
public class DataHandler<T> implements IHollywood <IDataStore<T>, T>
public class ActorImpl implements IActor extends DataHandler
public class DirectorImpl implements IDirector extends DataHandler
I have two points of confusion:
What is the correct signature for DataHandler? Currently I am using public class DataHandler<T> implements IHollywood <IDataStore<T>, T>
and I know I am passing T
as two parameters wrongly but can’t figure out the right way
What is the correct way for ActorImpl
and DirectorImpl
to implement IHollywood
and extend DataHandler
declaration?
Edit/Update:
I'll try to give more details:
// 1- support serialization
// provide interface to create/store/query persisted data
public interface IDataStore<ID> extends Serializable
public interface IHollywood<T extends IDataStore <ID>, ID>
// 2- provide an interface for Actor specific methods
public class Actor{}...
public interface IActor extends IHollywood <Actor, Long>
// Implement IActor
public class ActorImpl implements IActor
// 3- provide an interface for Director specific methods
public class Director{}...
public interface IDirector extends IHollywood <Director, Long>
// 4- Implement Director
public class DirectorImpl implements IDirector
// 5- Instead of CRUD, I’ll use a better name
// Implement retrieve()/store()/find methods
public class DataHandler <T> implements IHollywood <IDataStore<T>, T>
This way, I can use DataHandler to implement retrieve()/store()/find() methods of IHollywood
I can derive ActorImpl
and DirectorImpl
from DataHandler
to use its retrieve()/store()/find() methods
I know IActor
and IDirector
extend IHollywood
but I do not want to duplicate implementation of retrieve()/store()/find() methods of IHollywood
. I want the DataHandler
to implement them and ActorImpl
and DirectorImpl
to just inherit and use them, besides providing their own specific methods in their own implementation.
I hope this clarifies a bit what I need help with.
Upvotes: 3
Views: 1016
Reputation: 91
Thanks to @RC for solving this for me:
public class DataHander<I, T extends IDataStore<I>> implements IHollywood<T, I>
From his comment I learned how to fix the ActorImpl:
public class ActorImpl extends DataHander <Long, Actor> implements IHollywood
Solved.
Upvotes: 2