Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Java equivalent of the C# generic constraint "T,K: where T : It<K>"?

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()
like image 301
Alex Avatar asked Aug 10 '12 15:08

Alex


People also ask

Is Java similar to C?

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.

How different is C and C in Java?

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).

Is C C++ similar to Java?

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.

What is pointer equivalent in Java?

Java does not have pointers. It has references almost exactly as C++ has.


2 Answers

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.

like image 174
Chris Hannon Avatar answered Sep 21 '22 05:09

Chris Hannon


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();
    }
}
like image 25
corsiKa Avatar answered Sep 22 '22 05:09

corsiKa