Generic methods and inheritance

D

dwelzel

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

Lasse Reichstein Nielsen

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
 
D

Dale King

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

dwelzel

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

John C. Bollinger

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top