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?

5
  • 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? Commented Jul 1, 2018 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. Commented Jul 1, 2018 at 18:59
  • 2
    And why does that prevent you from typing that line of code in your editor/IDE and to compile it? Commented Jul 1, 2018 at 19:00
  • 2
    class BinarySearchTree<T> implements Comparable<T> {} 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 Strings). (Also, extends Comparable is a compilation error.) Commented Jul 1, 2018 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. Commented Jul 1, 2018 at 19:02

1 Answer 1

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>> {}
Sign up to request clarification or add additional context in comments.

4 Comments

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<T>. 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.
Aside: the construct Comparable<T> is almost always incorrect. Use Comparable<? super T> instead.
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
@Miguel Might want to look at stackoverflow.com/questions/2723397/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.