I would like to make something similar to Class<? extends MyParentClass>>
but using an interface, something like
Class<? implements MyInterfaceClass>
but i got this error:
- Incorrect number of arguments for type Class<T>; it cannot be parameterized with arguments <?,
MyInterfaceClass>
but It seems that is not possible ?
Yes, it is possible. But in world of generics there is no implements but only extends and super so simply use extends (even if you are working with interface).
Class<? extends MyInterfaceClass>
// ^^^^^^^
Example:
Class<? extends Runnable> taskClass;
You still use the word extends instead of implements
For example
public interface MyInterface {
}
public class Demo {
private Class<? extends MyInterface> myClass;
}
Related
For example,
public interface Foo<T extends Blackness, S extends T & Whiteness> { }
Error: Type parameter cannot be followed by other bounds
T extends Blackness and S extends T so S inherently extends Blackness. The only other contingency is that S must also extends Whiteness. Because of this restriction, S must be an extension of T but also implement the functionality of Whiteness. Because of this, you will likely have to provide the type T. It's not possible for S to have multiple bounded types, which is why a sub-interface is required that implements both. What you're trying to do doesn't make logical sense. Refer to this.
public interface Foo<T extends Blackness, S extends BlackAndWhiteness<T>> {
}
public interface BlackAndWhiteness<T extends Blackness> extends Whiteness, Blackness {
}
interface Blackness {
}
interface Whiteness {
}
I'm trying to use generics and a Supplier to avoid needing to typecast the result of my static factory method but failing. I think I'm close, can someone point out what I'm doing wrong here?
Here is the parameterized interface IManager:
public interface IManager<C extends IConfigObject> {
Here is an abstract base class AbstractManager, which includes the static factory method:
public abstract class AbstractManager<C extends IConfigObject> implements IManager<C> {
....
public static <C extends IConfigObject> AbstractManager<C> getInstance(Supplier<? extends AbstractManager<C>> supplier) {
Next, here is a concrete implementation called MyManager which is parameterized with MyConfigObject, which in turn implements IConfigObject:
public final class MyManager extends AbstractManager<MyConfigObject> {
And finally, the main() code:
Supplier<MyManager> supplier = MyManager::new;
MyManager manager = (MyManager) AbstractManager.getInstance(supplier);
If I don't have the cast in there, I get a compiler error:
Type mismatch: cannot convert from
AbstractManager to MyManager
Any suggestions?
Your problem is here: Supplier<? extends AbstractManager<C>>. By using the wildcard, you're saying "I don't care about the actual type" but you do.
The key here is to first use two type parameters, the second of which relies on the first one (I used M for "manager"):
public static <C extends IConfigObject, M extends AbstractManager<C>>
M getInstance(Supplier<M> supplier)
{
// ...
}
If you're not using C in the method body, you could refactor it to:
public static <M extends AbstractManager<? extends IConfigObject>>
M getInstance(Supplier<M> supplier)
{
// ...
}
I have a class defined as MyClass<T, S> for which I'd like to create a Comparator for MyClass where T and S extend Foo.
How can I go about doing this?
Unsuccessful attempts:
// Warning: The type parameter MyClass is hiding the type MyClass<T,S>
public class MyComparator<MyClass> implements Comparator<MyClass>
// Syntax errors and the warning from above
public class MyComparator<MyClass<T, S>> implements Comparator<MyClass<T, S>>
// Syntax errors
public class MyComparator<MyClass<T extends Foo, S extends Foo>> implements Comparator<MyClass<T extends Foo, S extends Foo>>
// Syntax errors and the warning from above
public class MyComparator<MyClass<? extends Foo, ? extends Foo>> implements Comparator<MyClass<? extends Foo, ? extends Foo>>
And various combinations of the above. What is the correct way? Thank you.
Your class is not generic. It always compares the same type of objects, ans this type is MyClass<? extends Foo, ? extends Foo>.
So it should be
public class MyComparator implements Comparator<MyClass<? extends Foo, ? extends Foo>>
I have the following generic class:
public class Evalutor<T>{
}
I would like to create the type called NumberEvalutor as follows:
public class NumberEvalutor<T> extends Evalutor<T extends Number>{ //Syntax error on token "extends", , expected
}
But I couldn't do it that way. Maybe you can advice another type-safe way?
Try with:
public class NumberEvalutor<T extends Number> extends Evalutor<T> {
}
Type parameters on class-level (like <T extends Number>) must be introduced after the class name and can be referred in the super-class/super-interface list. Otherwise, there won't be a way to (explicitly) specify their runtime value when creating class instances.
This one should work :)
public class NumberEvaluator<T extends Number> extends Evaluator<T> {
}
interface Foo<T extends Number>{
}
class Bar<T extends Number> implements Foo<T>{
}
Why does the class have to be written that way instead of:
class Bar<T extends Number> implements Foo<T extends Number>{
}
Surely the second way is clearer.
Because that's the same T, so it's redundant to say it extends Number again.
In the line
class Bar<T extends Number> implements Foo<T> {
T is defined at the first occurrence and used at the second. extends Number constrains the type that T can be instantiated with. You can put such constraints only at the place where T is defined.
It is similar to ordinary function parameters, where you write the type only in the declaration and not at the places where you use the parameter.