Why is implementing a generic interface so counter intuitive? - java

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.

Related

In an interface, I first declare a generic that extends an interface, then a generic that extends the first generic along with another interface

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 {
}

Java generics: Bound mismatch.Looking for the valid substitute

I have a couple of generic classes:
public interface Data<E> {}
public interface Clonable<E extends Clonable<E>> {}
public interface NaturalNumberInterface extends Data<NaturalNumberInterface> {}
public class NaturalNumber implements NaturalNumberInterface {}
public interface SetInterface<E extends Data<E>> extends Clonable<SetInterface<E>> {}
public class Set<E extends Data<E>> implements SetInterface<E> {}
When I'm trying to create the new instance of Set Set<NaturalNumber> s=new Set<NaturalNumber>(); compiler says:
NaturalNumber is not valid substitute for the type parameter <E extends Data<E>> of the type Set<E>
Maybe you can help me to find the mistake, cause I spent a long time and didn't find the solution.
I assume that your SetInterface is defined in the same way as ListInterface and Data is just interface Data<T>.
The generic argument of SetInterface is F-bounded: E extends Data<E>. In your current code NaturalNumber type extends Data<NaturalNumberInterface>. So if E is NaturalNumber, then condition is violated as it should extend more specific type Data<NaturalNumber>.
You should use F-bounds for NaturalNumberInterface as well:
public interface NaturalNumberInterface<T extends NaturalNumberInterface<T>> extends Data<T>
public class NaturalNumber implements NaturalNumberInterface<NaturalNumber>
This way it will work.

In java Generics, how to extend an interface instead of a class

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;
}

Generic type subclass

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> {
}

How to extend a generic class with added constraints in java?

I want to extend my BinaryTree class so that only Integer parameters are accepted and I can reuse the code.
public class BinaryTree<T extends Comparable<T>>
{/*code omitted for convenience*/}
public class BinaryTreeInt<T extends Integer> extends BinaryTree<T>
{/*code omitted for convenience*/}
I get following error on compilation-
BinaryTreeInt.java:1: type parameter T is not within its bound
public class BinaryTreeInt<T extends Integer> extends BinaryTree<T>
^
1 error
Can someone guide how to write code for such inheritance?
Integer is final so what you have is
public class BinaryTreeInt extends BinaryTree<Integer>
However the type implies its is int rather than Integer
The problem stems from the following potential class:
class FunnyNumber extends Integer { }
This class does not extend Comparable<T>, so it can't be used as the base T.
In other words, extends Comparable<Integer> is not the same as extends Comparable<T>.
Your second class should not be generic at all.
You should only use generics if you want to vary the type parameter.
Here, you want a single fixed type, so you should make a normal non-generic class that extends BinaryTree<Integer>. (or just use BinaryTree<Integer> directly and don't make a separate class at all)

Categories