Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Generic Object in java implements an interface

Tags:

java

generics

I want to have a generic object that implements an interface. I mean if i have a class A

class A<E> {
    E x;
}

I want to make sure that x will implement a particular interface(myInterface). In other words, that the type E implements an interface.

like image 876
ahmed elbagoury Avatar asked Mar 13 '10 23:03

ahmed elbagoury


2 Answers

class A<E extends MyInterface> {
    E x;
}

I initially thought you were looking for:

class A<E> implements MyInterface {
   E x;
}

or

class A<E> implements MyInterface<E> {
   E x;
}

as appropriate.

like image 174
bmargulies Avatar answered Nov 15 '22 15:11

bmargulies


class A<E extends MyInterface>
{

}

That's it, quite simple..

like image 21
Jack Avatar answered Nov 15 '22 13:11

Jack