2

how do I correctly write following class declaration in Java?

public class BinarySearchTree<T extends BinarySearchNode<E implements Comparable<E>>> implements Iterable<E>

Basically, I am trying to create a BinarySearchTree of any class T that inherits BinarySearchNode of a Comparable datatype E (And my BinarySearchTree should also be Iterable).

How can I properly declare that class in Java? Thanks!

RazK
  • 45
  • 8
  • 1
    Why does this thing take the *node type* instead of the *element type* as a type parameter? – user2357112 May 08 '17 at 16:49
  • Because I want classes which extend BinarySearchTree to be able to replace the BinarySearchNode with a different class that extends it. For example - An AVLTree would extend BinarySearchTree and use an AVLNode instead of the BinarySearchNode – RazK May 08 '17 at 16:57
  • Obviously, I'm not expecting anyone to give me the class implementation, If you read my question carefully you'd understand I'm only looking for the correct way to write the line I posted above. – RazK May 08 '17 at 16:59
  • Perhaps my phrasing was not clear, suggestions for a better way to put my question? – RazK May 08 '17 at 17:00
  • 1
    SIde question... Shouldn't your class be `Iterable` on the values i.e. `E` instead of on the node? – fps May 08 '17 at 19:24
  • 1
    @FedericoPeraltaSchaffner that's a good question! In fact I agree with you, will edit the question. Thanks! – RazK May 08 '17 at 19:27
  • 1
    Another suggestion... I would change `E extends Comparable` for `E extends Comparable super E>`. This would allow you to have a tree of i.e. `Dog` with `Dog` being a subclass of `Animal` and `Animal` being `Comparable`. I mean that you could use the `compareTo` method from one ancestor of the element type of your tree, and you shouldn't need to implement `compareTo` in `Dog` (using the one from `Animal` would suffice, if it's good enough). – fps May 08 '17 at 20:21
  • 1
    All this with that only change from `` to ` super E>`. If you are interested on this topic, you should read about [PECS](http://stackoverflow.com/questions/2723397/what-is-pecs-producer-extends-consumer-super). – fps May 08 '17 at 20:21
  • That's new to me, thank you!!! – RazK May 08 '17 at 22:09

2 Answers2

2
public class BinarySearchTree<T extends BinarySearchNode<E>, E extends Comparable<E>> implements Iterable<E>

The type parameter E needs to be defined separately from the node type, even if it ends up looking redundant: BinarySearchTree<BinarySearchNode<String>, String>. Java won't let you directly access a generic parameter of a generic type.

Also, note that generic parameters always use "extends" even with interfaces.

Sean Van Gorder
  • 3,393
  • 26
  • 26
0

With Generics in Java, you will only use extends. So, essentially your method decoration would become:

public class BinarySearchTree<T extends BinarySearchNode<E extends Comparable<E>>> extends Iterable<T>

Even for those classes that implement an interface, generics will use extends. The only other syntax for generics is super if you are looking at the inheritance from the opposite direction

CraigR8806
  • 1,584
  • 13
  • 21
  • 2
    This does not compile. You cannot nest generics' extends in a class definition, i.e. you cannot have `class A>`. Besides, `Iterable` is an interface, so you must use the keyword `implements` if the class acutally implements it, it cannot be extended. – fps May 08 '17 at 19:42