How to correctly extend and implement Java generic interfaces

Doc Martin

UPDATE:

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.

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 DataHander<T> implements IHollywood <IDataStore<T>, T>

public class ActorImpl implements IActor extends DataHander
public class DirectorImpl implements IDirector extends DataHander

I have two points of confusion:

  1. What is the correct signature for DataHander? Currently I am using public class DataHander<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 DataHander 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
// Implent retireve()/store()/find methods
    public class DataHander <T> implements IHollywood <IDataStore<T>, T>

1- This way, I can use DataHander to implement retireve()/store()/find() methods of IHollywood

2- I can derive ActorImpl and DirectorImpl from DataHander to use its retireve()/store()/find() methods

3- I know IActor and IDirector extend IHollywood but I do not want to duplicate implementation of retireve()/store()/find() methods of IHollywood. I want the DataHander to implant 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.

Doc Martin

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java interfaces and type polymorphism, one implement vs extend

From Dev

How do I correctly restrict a generic interface to extend Number class in Java and be able to run it in another class?

From Dev

How can a class extend and implement other classes and interfaces at the same time?

From Dev

How to implement a generic method in java

From Dev

Generic Types and Interfaces Java

From Dev

Generic Types and Interfaces Java

From Dev

How to correctly extend classes

From Dev

How to correctly implement equals(), hashCode() for Tree in Java?

From Dev

How to implement interfaces in MyHDL

From Dev

How to implement contradictory interfaces

From Dev

How to extend or implement classes?

From Dev

Subscription model with generics - implement multiple generic interfaces

From Dev

Subscription model with generics - implement multiple generic interfaces

From Dev

How to correctly create a bounded generic class that extends a generic interface in Java

From Dev

java how to extend a class while implement parent method

From Dev

How to get a sub-collection of a passed collection<generic type> where members extend or implement another?

From Dev

Java extend Generic Type Parameter

From Dev

How to correctly use ARC with interfaces?

From Java

Can an interface extend multiple interfaces in Java?

From Dev

FOSUserBundle how to extend handler correctly

From Dev

How to extend a Generic Collection in Delphi?

From Dev

How to implement iAd correctly

From Dev

How to extend generic class to be also generic

From Dev

How to implement a generic encoder?

From Dev

How to correctly implement a spring-websocket java client

From Dev

How can I implement my own generic collection in java?

From Dev

Java how to implement interface with a variadic method and generic return type

From Dev

How to implement Java method returning a generic type parametrized with array in Scala?

From Dev

How to just get the method in the implement class with a generic interface in Java

Related Related

  1. 1

    Java interfaces and type polymorphism, one implement vs extend

  2. 2

    How do I correctly restrict a generic interface to extend Number class in Java and be able to run it in another class?

  3. 3

    How can a class extend and implement other classes and interfaces at the same time?

  4. 4

    How to implement a generic method in java

  5. 5

    Generic Types and Interfaces Java

  6. 6

    Generic Types and Interfaces Java

  7. 7

    How to correctly extend classes

  8. 8

    How to correctly implement equals(), hashCode() for Tree in Java?

  9. 9

    How to implement interfaces in MyHDL

  10. 10

    How to implement contradictory interfaces

  11. 11

    How to extend or implement classes?

  12. 12

    Subscription model with generics - implement multiple generic interfaces

  13. 13

    Subscription model with generics - implement multiple generic interfaces

  14. 14

    How to correctly create a bounded generic class that extends a generic interface in Java

  15. 15

    java how to extend a class while implement parent method

  16. 16

    How to get a sub-collection of a passed collection<generic type> where members extend or implement another?

  17. 17

    Java extend Generic Type Parameter

  18. 18

    How to correctly use ARC with interfaces?

  19. 19

    Can an interface extend multiple interfaces in Java?

  20. 20

    FOSUserBundle how to extend handler correctly

  21. 21

    How to extend a Generic Collection in Delphi?

  22. 22

    How to implement iAd correctly

  23. 23

    How to extend generic class to be also generic

  24. 24

    How to implement a generic encoder?

  25. 25

    How to correctly implement a spring-websocket java client

  26. 26

    How can I implement my own generic collection in java?

  27. 27

    Java how to implement interface with a variadic method and generic return type

  28. 28

    How to implement Java method returning a generic type parametrized with array in Scala?

  29. 29

    How to just get the method in the implement class with a generic interface in Java

HotTag

Archive