Home > Archive > Java Help > April 2005 > Bounded generics
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| Hi -
I'm trying to extend Vector. I want it to accept any generic class
that implements a particular interface.
This is my best attempt:
interface MyInterface {
}
class Type1 implements MyInterface {
}
class Type2 implements MyInterface {
}
class MyVector<T implements MyInterface> extends Vector<T> {
}
I want MyVector to accept both Type1 and Type2 as classes but nothing
else which does not implement MyInterface.
I based this code on an O'Reilly book which apparently was written
before 1.5 was released. I've also tried wildcards without luck.
Thanks in advance.
| |
| Daniel Dyer 2005-04-22, 4:01 pm |
| On Fri, 22 Apr 2005 18:42:07 +0100, Brian <brian@notreal.com> wrote:
> Hi -
>
> I'm trying to extend Vector. I want it to accept any generic class
> that implements a particular interface.
>
> This is my best attempt:
>
> interface MyInterface {
> }
>
> class Type1 implements MyInterface {
> }
>
> class Type2 implements MyInterface {
> }
>
> class MyVector<T implements MyInterface> extends Vector<T> {
> }
>
> I want MyVector to accept both Type1 and Type2 as classes but nothing
> else which does not implement MyInterface.
>
> I based this code on an O'Reilly book which apparently was written
> before 1.5 was released. I've also tried wildcards without luck.
>
> Thanks in advance.
Try this:
class MyVector<T extends MyInterface> extends Vector<T> {
You have to use 'extends' rather than 'implements', even with interfaces.
This one got me the first time too.
Dan.
--
Daniel Dyer
http://www.footballpredictions.net
| |
|
| Daniel Dyer < dan@footballnospamformepleasepredictions
.net> wrote:
> Try this:
> class MyVector<T extends MyInterface> extends Vector<T> {
> You have to use 'extends' rather than 'implements', even with interfaces.
> This one got me the first time too.
> Dan.
That was it! Thanks Dan.
|
|
|
|
|