Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Bounded generics
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.



Report this thread to moderator Post Follow-up to this message
Old Post
Brian
04-22-05 09:01 PM


Re: Bounded generics
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

Report this thread to moderator Post Follow-up to this message
Old Post
Daniel Dyer
04-22-05 09:01 PM


Re: Bounded generics
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.



Report this thread to moderator Post Follow-up to this message
Old Post
Brian
04-23-05 01:58 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:25 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.