Forced parameterized type as constructor parameter must be correct

I searched for quite a while, and nothing really comes close to what I need.

Code example:

public class MyQueue<E extends Delayed & Serializable> extends DelayQueue<E> {
    private Class<E> mClass;
    public MyQueue(Class<E> type) {
        super();
        mClass = type;
    }
}

MyQueue created as: MyQueue q<MyObj> = new MyQueue<MyObj>(MyObj.class);.

My question is: how can I write a constructor in such a way that the "type" parameter has the correct parameterized type "E extends Delayed and Serializable"?

Hope I fully explained.

Thanks in advance for any help.

Change . From the answers and comments, at first I could not choose the correct answer. Therefore, for the first time, I will try to strengthen my question with what I wanted in the first place and what I have done now. For example, replying to the original message that I found.

, , E . E ( MyObj) . , - . , -.

, (MyObj OthrObj Delayed Serializable):

MyQueue q<MyObj> = new MyQueue<MyObj>(OthrObj.Class);

: , E E?

, , , , . , , , , , , .

+3
4

, , type Delayed & Serializable. , ...

+5

, "type" "E extends Delayed and Serializable".

public class MyQueue<E extends Delayed & Serializable> ...

.

public MyQueue(Class<E> type) {

. .

MyQueue<Long> myQueue = new MyQueue<Long>(Long.class);

. - .

+3

, :

public class MyQueue<E extends Delayed & Serializable> extends DelayQueue<E> {
    private E mClass;
    public MyQueue(E type) {
        super();
        mClass = type;
    }
}
+1
source
public MyQueue(Class<E> type) {
    super();
    mClass = type;
    type.asSubclass(Delayed.class);
    type.asSubclass(Serializable.class);//both of these throw when it can't happen
}

this explicitly checks the correctness of the passed class even when deleting the record type (and throws a classcast exception if it cannot be executed)

0
source

All Articles