Basic stupidity Java generics

  • Follow


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
0
Reply pjvleeuwen (13) 9/4/2008 7:58:52 PM

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 =3D new Container<Thing>();
It needs to be: IContainer<? extends IThing> var =3D new
Container<Thing>();

Again, sorry, and thanks for reading.

Cheers,
Paul

On 4 sep, 21:58, "pjvleeu...@gmail.com" <pjvleeu...@gmail.com> 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 {
> =A0 =A0 =A0 =A0 public interface IThing {
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 public interface IContainer<TYPE extends IThing> {
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 public class Thing implements IThing {
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 public class Container<TYPE extends Thing> implements
> IContainer<TYPE> {
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 public Test() {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 IContainer<IThing> var =3D new Container<=
Thing>();
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 public static void main(String[] args) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 new Test();
> =A0 =A0 }
>
> }
>
> $ javac Test.java
> Test.java:23: incompatible types
> found =A0 : Test.Container<Test.Thing>
> required: Test.IContainer<Test.IThing>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 IContainer<IThing> var =3D new Container<=
Thing>();
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0^
> 1 error

0
Reply pjvleeuwen (13) 9/4/2008 8:07:22 PM


pjvleeuwen@gmail.com wrote:

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

No problem, but thanks for posting back with the solution.

In the future, please do not top-post.

> Cheers,
> Paul
> 
> On 4 sep, 21:58, "pjvleeu...@gmail.com" <pjvleeu...@gmail.com> 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
> 

-- 
Sabine Dinis Blochberger

Op3racional
www.op3racional.eu
0
Reply no.spam6050 (307) 9/5/2008 8:25:54 AM

On Thu, 4 Sep 2008 12:58:52 -0700 (PDT), "pjvleeuwen@gmail.com"
<pjvleeuwen@gmail.com> 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
0
Reply see_website (4855) 9/6/2008 2:20:39 PM

Roedy Green <see_website@mindprod.com.invalid> writes:

> On Thu, 4 Sep 2008 12:58:52 -0700 (PDT), "pjvleeuwen@gmail.com"
> <pjvleeuwen@gmail.com> 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.'
0
Reply lrn.unread (114) 9/6/2008 4:01:40 PM

4 Replies
19 Views

(page loaded in 0.082 seconds)


Reply: