What is the equivalent in Java for this C# declaration ?
public class AsyncThreadPool<T, K> where T : IAsyncThread<K> {
and IAsyncThread
is an interface
public interface IAsyncThread<T>
{
T GetAsyncUsedObject();
void StartAsyncRequest();
}
I have tried :
public class AsyncThreadPool<T extends IAsyncThread<K>, K >
But is not correct, as T implements IAsyncThread<K>
not extends it.
And I need in this class to use T.StartAsyncRequest()
or similar
In C# it is:
T asyncThread = default(T);
asyncThread.StartAsyncRequest()
C is a middle-level language as it binds the bridges between machine-level and high-level languages. Java is a high-level language as the translation of Java code takes place into machine language, using a compiler or interpreter. C is only compiled and not interpreted. Java is both compiled and interpreted.
C is a compiled language that is it converts the code into machine language so that it could be understood by the machine or system. Java is an Interpreted language that is in Java, the code is first transformed into bytecode and that bytecode is then executed by the JVM (Java Virtual Machine).
As Java was inspired by C and C++, its syntax is similar to these languages. C++ is both a procedural and object-oriented programing language. Hence, C++ has features specific to procedural languages as well as features of object-oriented programming language. Java is a completely object-oriented programming language.
Java does not have pointers. It has references almost exactly as C++ has.
Generic constraints in Java only have two options, extends
and super
, and there is no difference in that case between extending and implementing. Your method declaration code should work as is.
For instantiation, you will have to do a pattern where you use the class of T
to create it. Because you can't directly use T.class
, you could create a factory, like corsiKa suggested, if you need reusability of the creation patterns, or you can just pass in a Class<T>
into the constructor for your class and keep that instance around.
public class AsyncThreadPool<T extends IAsyncThread<K>, K>{
private final Class<T> clazz;
public AsyncThreadPool(Class<T> clazz){
this.clazz = clazz;
}
public void Start() throws InstantiationException, IllegalAccessException{
T instance = clazz.newInstance();
instance.StartAsyncRequest();
}
}
It may also be important to note that, in C#, using default(T)
for a class or interface will produce a null reference and so your original example is incorrect.
Your <T extends IAsyncThread<K>>
is correct. Even though the class itself implements, not extends, the terminology for the generic definition is extends. If you wanted, you could use <T extends Object & IAsyncThread<K>>
or <T extends Object, IAsyncThread<K>>
but would be unnecessary.
For creating a member of type T, the only thing you really have at your disposal is to use a factory object.
public class AsyncThreadPool<T extends IAsyncThread<K>, K> {
private final AsyncThreadFactory<T> factory;
public ASyncThreadPool(AsyncThreadFactory<T> factory) {
this.factory = factory;
}
public void foo() {
T t = factory.createDefault();
t.startAsyncRequest();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With