Basic stupidity Java generics

Discussion in 'Java' started by pjvleeuwen@gmail.com, Sep 4, 2008.

  1. Guest

    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
    , Sep 4, 2008
    #1
    1. Advertising

  2. Guest

    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, "" <> 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
    , Sep 4, 2008
    #2
    1. Advertising

  3. Roedy Green Guest

    On Thu, 4 Sep 2008 12:58:52 -0700 (PDT), ""
    <> 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
    Roedy Green, Sep 6, 2008
    #3
  4. Roedy Green <> writes:

    > On Thu, 4 Sep 2008 12:58:52 -0700 (PDT), ""
    > <> 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.'
    Lasse Reichstein Nielsen, Sep 6, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Werpachowski
    Replies:
    6
    Views:
    284
    Markus Becker
    Jan 2, 2006
  2. andrew cooke
    Replies:
    1
    Views:
    263
    Chris Rebert
    Apr 20, 2011
  3. trans.  (T. Onoma)
    Replies:
    21
    Views:
    226
    Kristof Neirynck
    Jan 18, 2005
  4. Zachary Buckholz
    Replies:
    11
    Views:
    151
    G Klinedinst
    Jan 17, 2004
  5. Time Waster

    new variable stupidity

    Time Waster, Feb 23, 2008, in forum: Perl Misc
    Replies:
    2
    Views:
    83
    Ben Morrow
    Feb 25, 2008
Loading...

Share This Page