Question

I'm unable to understand the concept behind the subtyping / interface implementation in relation to generics. Please explain. I had the below structure:

`ICommand -> ProducerConsumerCommand`

IProducerConsumer -> SimpleProducerConsumer

My understanding is that any concrete class implementing IProducerConsumer must hold objects of type T which is the subclass of Number and ICommand interface.

IProducerConsumer<T extends Number,ICommand>

Now I want to write a implementation class SimpleProducerConsumer,

Now my intent is that I want to tell that SimpleProducerConsumer can hold any type parameters as defined in IProducerConsumer type parameters as below:

public class SimpleProducerConsumer<T,ICommand> implements Runnable,
        IProducerConsumer<T,ICommand> {

It dosen't work, it says as below:

SimpleProducerConsumer<T,ICommand> - ICommand complains (warning) that it is getting hidden.
IProducerConsumer<T,ICommand> - compiler errors in T saying as below

*Bound mismatch: The type T is not a valid substitute for the bounded parameter of the type IProducerConsumer<T,ICommand>*

So, My first question is why can't I define the same way as interface in concrete class. what is happening under the hoods.

if I can define

 public class test<ICommand> {}

why can't I do the same while implementing, confusing.

Now I define as below

public class SimpleProducerConsumer<T,ProducerConsumerCommand> implements Runnable,
        IProducerConsumer<T,ICommand> {

Still T in IProducerConsumer complains.

If I change as below:

public class SimpleProducerConsumer<T,ProducerConsumerCommand> implements Runnable,
        IProducerConsumer<T extends Number,ICommand> {

extends complains saying to remove the token.

Now i do like this as below:

public class SimpleProducerConsumer<T extends Number,ProducerConsumerCommand> implements Runnable,
        IProducerConsumer<T extends Number,ICommand> {

Still extends in IProducerConsumer dosen't work.

Now

public class SimpleProducerConsumer<T extends Number,ProducerConsumerCommand> implements Runnable,
        IProducerConsumer<T,ICommand> {

Yes now compiler is happy. Now I'm completely lost, what does above thing mean. SimpleProducerConsumer contains objects of type T which are subclass of Number and ProducerCommand object makes sense, but IProducerConsumer holds objects of Type T ??? what is this ?

What is the rationale behind this. Can someone please expalin.

Was it helpful?

Solution

You have two problems here:

  1. As it is declared now in IProducerConsumer<T extends Number,ICommand>, ICommand is not referencing your ICommand interface, but is instead just another generic type parameter declaration like T. Therefore you get the warning, that this generic type parameter is hiding your ICommand interface. You must declare your IProducerConsumer like so:

    public interface IProducerConsumer<N extends Number, C extends ICommand>
    
  2. You get a Bound mismatch for T, because T does not necessarily extend Number, as required by the IProducerConsumer interface. To fix this, you must declare your SimpleProducerConsumer like so:

    public class SimpleProducerConsumer<N extends Number, C extends ICommand> implements Runnable, IProducerConsumer<N,C>
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top