简体   繁体   中英

How to correctly extend and implement Java generic interfaces

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:

  1. 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

  2. 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>
  1. This way, I can use DataHandler to implement retrieve()/store()/find() methods of IHollywood

  2. I can derive ActorImpl and DirectorImpl from DataHandler to use its retrieve()/store()/find() methods

  3. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM