Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Basic stupidity Java generics

Reply
Thread Tools

Basic stupidity Java generics

 
 
pjvleeuwen@gmail.com
Guest
Posts: n/a
 
      09-04-2008
Hi all,
Probably I am doing something very twisted, but I keep getting an
"incompatible types" compile error and I don't know why. I brought it
back to the very simple script below. Could somebody please explain
me, why is this incorrect, it seems perfectly logical to me...
This is a big problem for me, because I want to hide all
implementation behind interfaces, and I need the return type
IContainer<Ithing> to be compatible with Container<Thing>. Apparently
it isn't.
Anyone? Many things for any help!
Cheers,
Paul


public class Test {
public interface IThing {
}
public interface IContainer<TYPE extends IThing> {
}
public class Thing implements IThing {
}
public class Container<TYPE extends Thing> implements
IContainer<TYPE> {
}
public Test() {
IContainer<IThing> var = new Container<Thing>();
}
public static void main(String[] args) {
new Test();
}
}


$ javac Test.java
Test.java:23: incompatible types
found : Test.Container<Test.Thing>
required: Test.IContainer<Test.IThing>
IContainer<IThing> var = new Container<Thing>();
^
1 error
 
Reply With Quote
 
 
 
 
pjvleeuwen@gmail.com
Guest
Posts: n/a
 
      09-04-2008
Damn! sorry, I found the problem. I spend two hours before posting the
question and I found the answer just minutes after the posting:
This is not correct: IContainer<IThing> var = new Container<Thing>();
It needs to be: IContainer<? extends IThing> var = new
Container<Thing>();

Again, sorry, and thanks for reading.

Cheers,
Paul

On 4 sep, 21:58, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
> Hi all,
> Probably I am doing something very twisted, but I keep getting an
> "incompatible types" compile error and I don't know why. I brought it
> back to the very simple script below. Could somebody please explain
> me, why is this incorrect, it seems perfectly logical to me...
> This is a big problem for me, because I want to hide all
> implementation behind interfaces, and I need the return type
> IContainer<Ithing> to be compatible with Container<Thing>. Apparently
> it isn't.
> Anyone? Many things for any help!
> Cheers,
> Paul
>
> public class Test {
> * * * * public interface IThing {
> * * * * }
> * * * * public interface IContainer<TYPE extends IThing> {
> * * * * }
> * * * * public class Thing implements IThing {
> * * * * }
> * * * * public class Container<TYPE extends Thing> implements
> IContainer<TYPE> {
> * * * * }
> * * * * public Test() {
> * * * * * * * * IContainer<IThing> var = new Container<Thing>();
> * * * * }
> * * * * public static void main(String[] args) {
> * * * * * * * * new Test();
> * * }
>
> }
>
> $ javac Test.java
> Test.java:23: incompatible types
> found * : Test.Container<Test.Thing>
> required: Test.IContainer<Test.IThing>
> * * * * * * * * IContainer<IThing> var = new Container<Thing>();
> * * * * * * * * * * * * * * * * * * * * *^
> 1 error


 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      09-06-2008
On Thu, 4 Sep 2008 12:58:52 -0700 (PDT), "(E-Mail Removed)"
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

>public interface IContainer<TYPE extends IThing> {

public class Container<TYPE extends Thing> implements

It is confusing and probably incorrect to redefine the meaning of TYPE

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      09-06-2008
Roedy Green <(E-Mail Removed)> writes:

> On Thu, 4 Sep 2008 12:58:52 -0700 (PDT), "(E-Mail Removed)"
> <(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
> said :
>
>>public interface IContainer<TYPE extends IThing> {

> public class Container<TYPE extends Thing> implements
>
> It is confusing and probably incorrect to redefine the meaning of TYPE


Might be confusing (although I personally don't think so, and would
just have called them both "T"), but it's definitly not incorrect.

It's just two unrelated type variables that happen to have the same
name. It's not more incorrect than having two different methods both
containing a variable called "i", or two methods both having an
argument called "x".

/L
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
generics depending on generics Soul VHDL 0 02-02-2009 09:14 AM
OT: Thursday Stupidity Briscobar MCSE 11 06-30-2005 10:28 PM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM
grasping a Usenet stupidity anthonyberet Computer Support 3 05-28-2004 06:30 AM
FINALLY FIXED (That's to the stupidity on my part) MatGyver Cisco 0 10-29-2003 09:48 PM



Advertisments