1

I tried commenting on the thread below itself, but I do not have enough rep points for it.
My simple question is: what is difference between the following two codes?

Please note: I am not sure if the former is even valid syntax, and maybe that's the answer.

public class BinarySearchTree<T> extends Comparable<T> {}

public class BinarySearchTree<T extends Comparable<T>> {}

Java : How do I implement a generic Binary Search Tree?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    *I am not sure if the former is even valid syntax*: then why don't you simply try to compile it before asking the question? – JB Nizet Jul 01 '18 at 18:57
  • Trying to understand how / what to code. It's a homework problem, and I am just having a hard time wrapping my head around the entire concept of binary search trees and sorting values. – Miguel Ortiz Jul 01 '18 at 18:59
  • 2
    And why does that prevent you from typing that line of code in your editor/IDE and to compile it? – JB Nizet Jul 01 '18 at 19:00
  • 2
    `class BinarySearchTree implements Comparable {}` is arguably strictly incorrect, and would break methods like `Arrays.sort`. Instances of `Comparable` are generally expected to be compared to other instances of the same type (i.e. a `String` compares with other `String`s). (Also, `extends Comparable` is a compilation error.) – Radiodef Jul 01 '18 at 19:01
  • That wasn't the basis for my question. But you're correct, I could've avoided that part of my question by compiling. Thanks for the tip. – Miguel Ortiz Jul 01 '18 at 19:02

1 Answers1

4
public class BinarySearchTree<T> extends Comparable<T> {}

The tree is Comparable and can hold any type. Tree objects would be compared by the type they hold. Maybe you are comparing only the root elements of the trees?
This is not valid because Comparable is an interface; you cannot extend classes from interfaces.

public class BinarySearchTree<T extends Comparable<T>> {}

The tree can only contain Comparable types. This is valid.

You could combine the two into the following:

public class BinarySearchTree<T extends Comparable<T>> implements Comparable<BinaryTree<T>> {}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thank you! In this case, we are saying: a class BinarySearchTree of type T is a child of class Comparable, which also holds data type T. Additionally, this is implementing an interface Comparable of data type BinarySearchTree. I won't lie. This is a lot to wrap my head around. But I'll keep thinking / reading about it if I am on the right track. Thanks again for your prompt help. – Miguel Ortiz Jul 01 '18 at 19:06
  • 1
    Aside: the construct `Comparable` is almost always incorrect. Use `Comparable super T>` instead. – markspace Jul 01 '18 at 19:07
  • 1
    I would say "contains type T, which are Comparable", because the type of of the class is always a BinaryTree. Also, Comparable is not a class – OneCricketeer Jul 01 '18 at 19:09
  • 1
    @Miguel Might want to look at https://stackoverflow.com/questions/2723397/what-is-pecs-producer-extends-consumer-super – OneCricketeer Jul 01 '18 at 19:11