Answer the question
In order to leave comments, you need to log in
Passing to the constructor of any object?
A question. How to write constructor arguments so that any object that implements the desired interface is accepted?
in theory it should be something like this
public MyClass(<? extends MyInterface> obj){
...
}
Answer the question
In order to leave comments, you need to log in
It is possible like this:
public <T extends SomeInterface> MyClass(T object) {
}
This is called generics.
class MyClass<T extends MyInterface>{
private T val;
public MyClass(T obj){
val= obj;
...
}
}
public MyClass(MyInterface obj){
...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question