title: How to Write a generic method to find the maximal element and invoke that method?
body: <p>While I was trying to solve exercise <a href="http://docs.oracle.com/javase/tutorial/java/generics/QandE/generics-answers.html" rel="nofollow">from generics tutorial Q&amp;A</a> My answers were slightly different   </p>
  
  <p><strong>My Answers</strong></p>
  
  <pre><code>public static &lt;T extends Comparable&lt;? super T&gt;&gt;
      T max(List&lt;? extends T&gt; list, int begin, int end) //Option1
  
  public static &lt;T extends Comparable&lt;T&gt;&gt;
      T max(List&lt;? extends T&gt; list, int begin, int end) //Option2
  </code></pre>
  
  <p>from quoted answer below</p>
  
  <p>So My question is </p>
  
  <ul>
  <li><p>Option1 :Would it make any difference if <code>T extends Object &amp; Comparable&lt;? super T&gt;</code> is replaced with <code>T extends Comparable&lt;? super T&gt;</code>. Isn't <code>extends Object</code> implicit ?</p></li>
  <li><p>Option2 :Would it make any difference if <code>Comparable&lt;? super T&gt;</code> is replaced with <code>Comparable&lt;T&gt;</code> ? if so How ?</p></li>
  <li><p>Eclipse code completion creates local variable <code>List&lt;? extends Comparable&lt;? super Comparable&lt;? super T&gt;&gt;&gt; list;</code> on Ctrl+1 <code>max(list, 1, 10);</code> which is bit lengthy. How to Define a classes (hierarchy) that extends <code>Comparable&lt;? super T&gt;</code> , create list and add instances to the list and invoke below method ? Basically I want to know how to invoke <code>max()</code> after adding class instances <code>A or B</code>  into a list where <code>class B extends A</code> </p></li>
  </ul>
  
  <hr>
  
  <blockquote>
    <p>Write a generic method to find the maximal element in the range
    [begin, end) of a list.</p>
    
    <p>Answer:</p>
  </blockquote>
  
  <pre><code>import java.util.*;
  
  public final class Algorithm {
      public static &lt;T extends Object &amp; Comparable&lt;? super T&gt;&gt;
          T max(List&lt;? extends T&gt; list, int begin, int end) {
  
          T maxElem = list.get(begin);
  
          for (++begin; begin &lt; end; ++begin)
              if (maxElem.compareTo(list.get(begin)) &lt; 0)
                  maxElem = list.get(begin);
          return maxElem;
      }
  }
  </code></pre>