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.