2

This works as intended - the (non static) inner class Node is parametrized with the Tree's params (K, V):

public class Tree<K,V> {

    private abstract class Node extends Page<K,V> {}

    private final class InnerNode extends Node {}

    private final class LeafNode extends Node {}
}

Now I want to somehow convey to the compiler that the inner Nodes should accept only Integers as parameters. Tried:

private abstract class Node<V> extends Page<K, V> {}

but it displays the warning:

The type parameter V is hiding the type V

I want to achieve:

public class Tree<K, V> {

    /** The type parameter V is hiding the type V */
    private abstract class Node<V> extends Page<K, V> {}

    private final class InnerNode extends Node<Integer> {}

    private final class LeafNode<V> extends Node<V> {}
}

but with V being the Tree's type param. Below won't compile:

public class Tree<K, V> {

    private abstract class Node<T extends V> extends Page<K, V> {}

    /**
     * Bound mismatch: The type Integer is not a valid substitute for the
     * bounded parameter <T extends V> of the type Tree<K,V>.Node<T>
     */
    private final class InnerNode extends Node<Integer> {}

    private final class LeafNode<T extends V> extends Node<T> {}
}

Is what I want to do even possible ? Or a gross misunderstanding of generics ?

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
  • Your implementation says that `T` on `Node` must be a subclass of `V`, but `Integer` is not known to be a subtype of `V`, which is why the declaration is not legal. If you look at the base classes, they don't really make sense: a `Page` cannot be represented as a `Page`. There is no known relationship between `Integer` and `V`. What are you trying to achieve? – Mike Strobel Jun 06 '14 at 20:20
  • @MikeStrobel: I try to achieve what I say in the listing 3 - I know why it does not compile but I wonder if there are any clever bounds that would do the trick I try in listing 4 – Mr_and_Mrs_D Jun 06 '14 at 20:21
  • There is no way to make the compiler believe that `Integer` extends `V` if `V` is declared as an unbounded type variable. It will not believe that because it would not be true unless `V` was *always* `Integer`, in which case the type variable would be pointless. – Mike Strobel Jun 06 '14 at 20:28
  • @MikeStrobel: thanks - see the answer by Gábor Bakos- to this direction maybe ? – Mr_and_Mrs_D Jun 06 '14 at 20:40

1 Answers1

2
public class Tree<K, V> {

    private abstract class Node<T> extends Page<K, T> {}

    private final class InnerNode extends Node<Integer> {}

    private final class LeafNode extends Node<V> {}
}

It's all so simple and magnificent really - beauty bare and all that :D

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361