I don’t know how to give this a better title as I don’t really know what this pattern is called in Java.
Right now I have a method with this signature:
public Directory getDirectory(Class<? extends Directory> type) { ... }
And you call it like this:
MyDirectory directory = (MyDirectory)getDirectory(MyDirectory.class);
The constraint on the type ensures that MyDirectory must derive from Directory.
What I really want to do is avoid the cast and reduce the amount of code required. In C# you could say:
MyDirectory directory = getDirectory<MyDirectory>();
Is there a way to do this or something similar in Java? I haven’t coded any Java since version 1.4!
1 Answer