Member variable which must extend class A and implement some interface

jtedit

I need to have a variable in a class which is an instance of class "ClassA" but which also implements interface "InterfaceI".

Obviously this can be done for one or the other easily:

private ClassA mVariable;
private InterfaceI mVaraible;

but how can I enforce the object both extends ClassA and implements InterfaceB? Something like:

private <? extends ClassA & InterfaceI> mVaraible;

is what I need but I have no idea of the syntax or if it is even possible. I will also need a get and set method, but that can be done with generics (I think?)

The obvious solution is

public class ClassB extends ClassA implements InterfaceI{
    //whatever here
}

However both InterfaceI and ClassA are part of an external libary, this libary has classes which extend ClassA and InterfaceI and I cant edit them to make them extend ClassB, therefore this solution will not work.

jtedit

I have found a rather hacky workaround.

The class contains both a ClassA and InterfaceI reference as follows:

private ClassA mItemClassA;
private InterfaceI mItemInterfaceI;

The set method then looks like this:

public void setVaraible(ClassA item){
    assert (item instanceof InterfaceI);
    mItemClassA = item;
    mItemInterfaceI = (InterfaceI) item;
}

Other variations of this could be throwing an exception, returning false, etc if the item is not an instance of InterfaceI

Other methods in the class can then call functions from both InterfaceI and ClassA using mItemClassA and mItemInterfaceI.

I am not going to implement a get method for now, but if I did there would likely have to be a version to get the interface and a version to get the class.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Member variable which must extend class A and implement some interface

From Dev

Implement interface in class or as member

From Dev

Interface/class/implement/extend confusion

From Dev

Class does not implement interface member

From Dev

How to implement an interface using class member?

From Dev

Fighter class does not implement interface member

From Dev

Specify that a Class argument must implement a particular interface

From Dev

"Class does not implement interface member" error on class implementing a generic interface

From Dev

How to implement Interface to an abstract class which is constraint?

From Dev

How to implement Interface to an abstract class which is constraint?

From Dev

Must a class adhere to the documented contract of an interface to be said to implement that interface

From Dev

Argument must extend class

From Dev

Xamarin jar binding error: Class does not implement interface member

From Dev

Extend AND implement in the same class

From Dev

Extend AND implement in the same class

From Dev

Why do we re-implement parent interface of derived interface on class which implement derived interface

From Dev

Can I have a class field that is the Class<?> of a type that must extend another class and an interface?

From Dev

Does a class which implements an interface's method (without explicitly implementing that interface) extend that specific interface?

From Dev

how to extend scope of some variable in anonymous inner class

From Dev

extend a class to conform to an interface?

From Dev

extend a class to conform to an interface?

From Dev

limit T in a generic class to types that implement some interface

From Dev

Realm model classes must either extend RealmObject or implement RealmModel to be considered a valid model class

From Dev

Sort vector of class objects based on some member variable

From Dev

Swift: How to declare a static member variable which is a class

From Dev

Does not implement interface member issues

From Dev

Error: Does not implement interface member

From Dev

Error: Does not implement interface member

From Dev

Does not implement interface member MVC

Related Related

  1. 1

    Member variable which must extend class A and implement some interface

  2. 2

    Implement interface in class or as member

  3. 3

    Interface/class/implement/extend confusion

  4. 4

    Class does not implement interface member

  5. 5

    How to implement an interface using class member?

  6. 6

    Fighter class does not implement interface member

  7. 7

    Specify that a Class argument must implement a particular interface

  8. 8

    "Class does not implement interface member" error on class implementing a generic interface

  9. 9

    How to implement Interface to an abstract class which is constraint?

  10. 10

    How to implement Interface to an abstract class which is constraint?

  11. 11

    Must a class adhere to the documented contract of an interface to be said to implement that interface

  12. 12

    Argument must extend class

  13. 13

    Xamarin jar binding error: Class does not implement interface member

  14. 14

    Extend AND implement in the same class

  15. 15

    Extend AND implement in the same class

  16. 16

    Why do we re-implement parent interface of derived interface on class which implement derived interface

  17. 17

    Can I have a class field that is the Class<?> of a type that must extend another class and an interface?

  18. 18

    Does a class which implements an interface's method (without explicitly implementing that interface) extend that specific interface?

  19. 19

    how to extend scope of some variable in anonymous inner class

  20. 20

    extend a class to conform to an interface?

  21. 21

    extend a class to conform to an interface?

  22. 22

    limit T in a generic class to types that implement some interface

  23. 23

    Realm model classes must either extend RealmObject or implement RealmModel to be considered a valid model class

  24. 24

    Sort vector of class objects based on some member variable

  25. 25

    Swift: How to declare a static member variable which is a class

  26. 26

    Does not implement interface member issues

  27. 27

    Error: Does not implement interface member

  28. 28

    Error: Does not implement interface member

  29. 29

    Does not implement interface member MVC

HotTag

Archive