generics: is this possible and with what syntax??

J

Joost Kraaijeveld

Hi,

I want to use the concrete type of an outer class as the concrete type
of an inner class but if I do so, I get a warning ( in Eclipse, for some
sample code see below):

"The type parameter ConcreteType is hiding the type ConcreteType"

In other places in my code, in the class outerClass, the member "object"
of the InnerClass is not recognized as being of type ConcreteType, but
*only* as of type AbstractBaseClass, so I assume that warning actaully
means something ;-).

Is what I want possible and if so, what should be the right syntax?


Sample code:

public class OuterClass < ConcreteType extends AbstractBaseClass > {
// Next line gives the warning
class InnerClass < ConcreteType extends AbstractBaseClass > {
ConcreteType object;
}
}

TIA

Joost
 
J

Jaakko Kangasharju

Joost Kraaijeveld said:
I want to use the concrete type of an outer class as the concrete type
of an inner class but if I do so, I get a warning ( in Eclipse, for some
sample code see below):

public class OuterClass < ConcreteType extends AbstractBaseClass > {
// Next line gives the warning
class InnerClass < ConcreteType extends AbstractBaseClass > {
ConcreteType object;
}
}

Shouldn't you just remove the type parameter from the inner class,
i.e.,

public class OuterClass < ConcreteType extends AbstractBaseClass > {
class InnerClass {
ConcreteType object;
}
}

Because if you want the types to be the same, obviously you need to
share the type. In your example you could use different classes as
parameters to OuterClass and InnerClass.
 
T

Thomas Hawtin

Joost Kraaijeveld wrote::
"The type parameter ConcreteType is hiding the type ConcreteType"
public class OuterClass < ConcreteType extends AbstractBaseClass > {
// Next line gives the warning
class InnerClass < ConcreteType extends AbstractBaseClass > {
ConcreteType object;
}
}

You are defining ConcreteType twice. If InnerClass were a static nested
class, then a different name would be preferable. As it is, you can just
write:

public class OuterClass <CONCRETE extends AbstractBaseClass> {
class InnerClass {
CONCRETE object;
}
}
class AbstractBaseClass {}

Tom Hawtin
 
J

Joost Kraaijeveld

Hi Jaakko,

Jaakko said:
Shouldn't you just remove the type parameter from the inner class,
i.e.,

public class OuterClass < ConcreteType extends AbstractBaseClass > {
class InnerClass {
ConcreteType object;
}
}

Because if you want the types to be the same, obviously you need to
share the type. In your example you could use different classes as
parameters to OuterClass and InnerClass.
Yes, that did it. Thanks for the answer.

Joost
 

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

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,037
Latest member
MozzGuardBugs

Latest Threads

Top