0

I Have below the constructor and private paramter.

private Class class; public SomeRequest(subClass_of_SomeClass.java){ this.class=subClass_of_above_parameter; } 

Now the constructor should not accept any class type. It has to accept the class type which is a sub class of SomeClass.java.

How can i write a generic parameter?

private Class<SomeClass> class; 

Is above declaration correct?

2 Answers 2

1

you can use something like:

private class Class<T extends SomeClass> { ... } 

or

private class Class<? extends SomeClass> { ... } 

(editted for the sake of correctness, in case someone would need to compile it ;))

Sign up to request clarification or add additional context in comments.

6 Comments

Kamil, my requirement is it should accept only SomeClass sub classes and not SomeClass.
so I don't think that it's syntactically possible, you could only chnage classes, so that there is one more class like class Extended extends SomeClass, and all current subclasses should extend this added one - then you could do private Class<? extends ExtendedSomeClass> class;
Kamil, wt is the difference between above two lines and which one is recommended?
you use T only when you declare generic type on the class
private Class<T extends SomeClass> class; is not valid Java syntax
|
0

Best way of doing it will be the way @Kamil explained but if you specifically want to use subclasses then Declare SomeClass as abstract or declare it as Interface.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.