0

Defining a class like

public class MyRandomList<T extends Comparable>{
    List<T> randomList;
}

means we can create a MyRandomList instance with any class that implements Comparable.

MyRandomList<ComparableStudent> l = new MyRandomList<ComparableStudent>();

Where

public class ComparableStudent implements Comparable<ComparableStudent>{

}

Should't the syntax have been <T implements Comparable> ?
Or am I understanding something fundamentally wrong about Java?

A Nice Guy
  • 2,538
  • 3
  • 27
  • 50
  • Check this link. https://stackoverflow.com/questions/4343202/difference-between-super-t-and-extends-t-in-java – Sambit May 25 '19 at 09:38
  • 1
    @ArnabBhagabati your question is nice, but it's already been asked. I was preparing my answer for you, but the question was closed (and I fully agree with @Ole). I posted it in another thread (https://stackoverflow.com/a/56304595/4922375), I hope it will be helpful for you. – Andrew Tobilko May 25 '19 at 12:00

1 Answers1

1

Simply put: Java does not make a distinction between interfaces and classes when defining bounds for generic type parameters. extends is used for both interface and class types which makes expressing the bound direction (link added from Sambits comment on the main post) more streamlined.

roookeee
  • 1,664
  • 10
  • 19