Comparable and generics - java

When this class is created..
public static class TreeNode<E extends Comparable<E>>
what does the <E extends Comparable<E>> mean?

That is a generic constraint.
It means that whatever type you store in the TreeNode must implement the Comparable<E> interface.

It means that whenever you create an instance of this class like
TreeNode<MyClass> myTreeNode = new TreeNode<MyClass>();
MyClass must implement Comparable < MyClass >
public class MyClass implements Comparable<MyClass>
{
//CODE
}

Related

How to implement a generic interface where type param is Enum and some other type

I'm working with an interface that accepts a type parameter:
public interface Container<T>
Now I can have a class as such that implements it:
public class EnumContainer implements Container<Enum>
But now suppose I want a Container for enums that implements a interface called Position:
public interface Position {
String getAbbreviation();
String getDescription();
}
How can I define PositionEnumContainer? I've tried this but it's a compile time error:
public class PositionEnumContainer implements Container<Enum & Position>
I do not want to make PositionEnumContainer generic such as:
public class PositionEnumContainer <T extends Enum<T> & Position> implements Container<T>
You can make PositionEnumContainer implement Container<Enum<? extends Position>>.
If a class extends Enum<? extends Position> then it's an enum class that also implements Position, therefore, PositionEnumContainer will only accept objects that are both of type Enum and of type Position. But unfortunately, Java doesn't know about it, so PositionEnumContainer's producing methods will return Enum<? extends Position>, that can't be implicitly cast back to Position, and you will have to cast it yourself or use safe Enums#narrow method.
public class Enums {
#SuppressWarnings("unchecked") // only class E can extend Enum<E>
public static <E extends Enum<E>> E narrow(Enum<E> obj) {
return (E) obj;
}
}
Alternatively, you can create the PositionEnum interface that extends Position and can only be implemented by enums and then make PositionEnumContainer implement Container<PositionEnum>. In this case, each ConcretePositionEnum will have to implement PositionEnum<ConcretePositionEnum> instead of Position.
// should only be implemented by enums
// you can verify it with annotation processor
public interface IEnum<E extends Enum<E>> extends Comparable<E> {
String name();
int ordinal();
Class<E> getDeclaringClass();
#SuppressWarnings("unchecked")
default E self() { return (E) this; }
}
public interface PositionEnum<E extends Enum<E> & Position> extends IEnum<E>, Position {}

Java collection wildcard with multiple bounds (implements interface and extends class)

Is something like this possible?
public class TypedPreferenceScreen<T> {
private ArrayList<? super Preference extends TypedPreference<T>> childPrefs;
}

How to extend Node<T extends Comparable<T>> class

I have an abstract class that is like this:
public abstract class Node<T extends Comparable<T>>
I need extends Comparable<T> so that I may use .compareTo() with generic type T.
I try to extend this class with this sub-class declaration:
public class ListNode<T> extends Node<T extends Comparable<T>>
However this throws SyntaxErrors in eclipse for the second extends and for the final two >>.
Is this the way to declare the sub-class? It seems so messy.
public class ListNode<T extends Comparable<T>> extends Node<T extends Comparable<T>>
How should this be done? Cheers in advance.
With Node as
public abstract class Node<T extends Comparable<T>>{}
ListNode can be:
public class ListNode<T extends Comparable<T>> extends Node<T>
The key thing to note is that the type parameter after the name of the declared class is what constrains T for everything else in the class. This includes the other types extended by the declared class.
This means that once you have bounded T you don't need to do it again in the extends/implements statements.

Implementing a Generic Interface In Java

I am new to Java Generics. I have to implement an interface which is of generic type. The syntax is as follows:
public interface A{}
public interface B<T extends A>{
public T methodB(T a) ;
}
Now I have to implement B so Lets say my Class is C
public class C implements B<T extends A>{}
The java compiler is not letting me use it this way. Also I do not want to use raw types. Please help.
It should be
public class C<T extends A> implements B<T>
The type parameter is declared following the class name, and later can be used in the implements clause.
If your implementing class is still a generic type you have to use this syntax:
public class C<T extends A> implements B<T> {}
As explained by Eran.
If C is not generic you simply need to specify the type argument for your interface:
public class C implements B<TypeExtendingA> {}
Where TypeExtendingA implements or extends A (or is A)

Type check parameter is a (sub)class and also implements an interface

This answer no doubt exists on SO, but I haven't found the right combination of search terms to come up with it.
I have a method that I want to take a parameter that is of class A, but also implements interface B. How do I do it?
e.g.
public class MySubclassWithInterface extends MyClass implements MyInterface { }
public class MySubclass extends MyClass { }
public class MyInterfaceClass implements MyInterface { }
public class MyOtherSubclassWithInterface extends MyClass implements MyInterface { }
Out of the three classes above, I only want my method to accept an object that is MyClass and implements MyInterface, in other words, either MySubclassWithInterface or MyOthersubclassWithInterface but not MySubclass or MyIntefaceClass
I very sheepishly tried the following which obviously failed:
public void myMethod( (MyClass MyInterface) parameterName) {
...
}
Thanks for your help in advance.
You can express this with a generic type as in the following signature:
<T extends MyClass & MyInterface> void m(T p)
The rule is that the first type must be a class or an interface and any following parameter must be an interface type.

Categories

Resources