Generic methods and inheritance

Discussion in 'Java' started by dwelzel@yahoo.com, Jul 1, 2005.

  1. Guest

    I've been having some trouble with generic methods and inheritance.
    Suppose I have an interface that looks like:

    interface A {

    public <T extends MyClass> void foo(T input);
    }


    class MyClassDerivative extends MyClass { ... }



    Finally:

    class B implements A {

    public void foo(MyClassDerivative input);
    }


    -----------------------

    This doesn't work and I'm wonder if it is possible to express this idea
    using generics. In the interface I want to say, you need to define a
    method which takes this type, or a derivative of it and not force
    derived classes into casting into their derived type.
     
    , Jul 1, 2005
    #1
    1. Advertising

  2. writes:

    > interface A {
    > public <T extends MyClass> void foo(T input);
    > }


    > class MyClassDerivative extends MyClass { ... }


    > class B implements A {
    > public void foo(MyClassDerivative input);
    > }


    >
    > This doesn't work and I'm wonder if it is possible to express this idea
    > using generics.


    No. You are allowed to widen the types of parameters in a specialization,
    and narrow the return type in Tiger too, but what you are doing is
    narrowing the set of parameters that can be used in the specializing class.

    If you have an instance of B, call it "b", then you should be able to
    do:

    A a = b; // substitution
    a.foo<MyClass>(new MyClass());

    The call matches the interface A, but it doesn't match the signature of
    any method in B, so you can't say that B implements A.

    > In the interface I want to say, you need to define a
    > method which takes this type, or a derivative of it and not force
    > derived classes into casting into their derived type.


    Why not just:
    void foo(MyClass input);
    Or am I misunderstanding the question?
    /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, Jul 1, 2005
    #2
    1. Advertising

  3. Dale King Guest

    wrote:
    > I've been having some trouble with generic methods and inheritance.
    > Suppose I have an interface that looks like:
    >
    > interface A {
    >
    > public <T extends MyClass> void foo(T input);
    > }
    >
    >
    > class MyClassDerivative extends MyClass { ... }
    >
    >
    >
    > Finally:
    >
    > class B implements A {
    >
    > public void foo(MyClassDerivative input);
    > }
    >
    >
    > -----------------------
    >
    > This doesn't work and I'm wonder if it is possible to express this idea
    > using generics. In the interface I want to say, you need to define a
    > method which takes this type, or a derivative of it and not force
    > derived classes into casting into their derived type.


    There's something like this, but I'm not sure it does what you are
    asking for:

    interface A<T extends MyClass> {
    public void foo(T input);
    }

    class MyClassDerivative extends MyClass { }

    class B implements A< MyClassDerivative > {

    public void foo(MyClassDerivative input)
    {
    }
    }

    As Lasse points out you can't narrow parameters in subclasses. This
    version works by effectively "narrowing" the interface before subclassing.


    --
    Dale King
     
    Dale King, Jul 1, 2005
    #3
  4. Guest

    Thanks for the info. This is how I solved the problem, but didn't like
    making the interface generic when it was really only one method that
    needed the generic type. I guess this will do for now.
     
    , Jul 1, 2005
    #4
  5. wrote:
    > Thanks for the info. This is how I solved the problem, but didn't like
    > making the interface generic when it was really only one method that
    > needed the generic type. I guess this will do for now.


    You seem to be missing an important point: your interface defined a
    generic method, so any implementation must therefore implement that
    generic method -- *as a generic method*. The class does not otherwise
    implement the interface. You did not want your implementation classes
    to have generic methods, so even though the interface only has one
    method that makes use of the type parameter, it apparently *is* a
    generic interface you want, not a normal interface with a generic method.

    --
    John Bollinger
     
    John C. Bollinger, Jul 2, 2005
    #5
    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. Murat Tasan
    Replies:
    1
    Views:
    8,057
    Chaitanya
    Feb 3, 2009
  2. Replies:
    2
    Views:
    440
  3. minlearn
    Replies:
    2
    Views:
    461
    red floyd
    Mar 13, 2009
  4. Rouslan Korneychuk
    Replies:
    8
    Views:
    605
    Rouslan Korneychuk
    Feb 10, 2011
  5. Kenneth McDonald
    Replies:
    5
    Views:
    329
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page