title: Why does my method override give a type clash?
body: <p>I need help to understand how the testing should go through. I should implement the test method in <code>Stacktest.java</code> (have already done that). Then extend <code>StackTest</code> with <code>LinkedlistTest</code> (I have already done that). Then add super.setUp() as the first line in <code>LinkedListTest.setUp</code> (I have done that).</p>
  <p>But the one overriden method is <code>getIntegerStack</code> that I implement in the <code>LinkedListTest</code>, but then I get an error:</p>
  <pre><code>'getIntegerStack()' in 'LinkedListTest' clashes with 'getIntegerStack()' in 'StackTest'; attempting to use incompatible return type
  </code></pre>
  <p>I dont know how to fix it. The code:</p>
  <ul>
  <li><code>StackTest.java</code> is an abstract test class.</li>
  <li>For each implementation of <code>Stack</code>, one may simply extend <code>StackTest</code> with an implementing test class. The only method that should be overridden is <code>StackTest.getIntegerStack</code>, which should simply return an instance of a class that implements <code>Stack&lt;Integer&gt;</code>. See the <code>setUp</code> method in <code>StackTest</code> and try to understand how this works.</li>
  <li>In your case, you should extend <code>StackTest</code> with a test class called <code>LinkedListTest</code>.</li>
  <li>you must add a call to <code>super.setUp()</code>; as the first line in <code>LinkedList.setUp()</code>.</li>
  </ul>
  <p>TODO: How should I make the <code>getIntegerStack()</code> do work.</p>
  <pre><code>import org.junit.Test;
  import org.junit.Before;
  import static org.junit.Assert.*;
  
  import static org.hamcrest.MatcherAssert.assertThat;
  import static org.hamcrest.CoreMatchers.*;
  import java.util.Arrays;
  import java.util.NoSuchElementException;
  import java.util.Stack;
  
  /**
   * Test class for LinkedList
   */
  public class LinkedListTest extends StackTest{
  
      /* A sequence of integers */
      private int[] elements;
  
      /* An empty linked list */
      private LinkedList&lt;Integer&gt; list;
  
      @Before
      public void setUp() {
          super.setUp();
          list = new LinkedList&lt;Integer&gt;();
          elements = new int[]{-919,  388,   67, -248, -309, -725,  904,   53,
                  90, -469, -559,  256,  612,  366, -412, -221,
                  347, -921, -978,  324, -858,  480, -443,  891,
                  329,   -5,  878, -538,  445, -366,  760,   52};
      }
  
      /**
       * This is the only method that implementing classes need to override.
       *
       * @return An instance of Stack.
       */
      @Override
      protected Stack&lt;Integer&gt; getIntegerStack() {
          return null;
      }
  }
  </code></pre>
  <p>Stack interface:</p>
  <pre><code>  * A interface of stack
    */
   public interface Stack &lt;T&gt; {
       /**
        * Adds the element to the top of the stack.
        */
       void push (T elem);
      
        T pop();
  
        T top();
  
       int size();
  
       boolean isEmpty();
   }
  </code></pre>
  <pre><code>import java.util.EmptyStackException;
  import java.util.NoSuchElementException;
  /**
   * A singly linked list.
   *
   */
  public class LinkedList&lt;T&gt; implements Stack &lt;T&gt; {
      private ListElement&lt;T&gt; first;   // First element in list.
      private ListElement&lt;T&gt; last;    // Last element in list.
      private int size;  // Number of elements in list.
  
      /**
       * A list element.
       */
      private static class ListElement&lt;T&gt;{
          public T data;
          public ListElement&lt;T&gt; next;
  
          public ListElement(T data) {
              this.data = data;
              this.next = null;
          }
      }
  
      /**
       * Creates an empty list.
       */
      public LinkedList() {
          this.first = null;
          this.last = null;
          this.size = 0;
      }
  
      /**
       * Inserts the given element at the beginning of this list.
       *
       * @param element An element to insert into the list.
       */
      public void addFirst(T element) {
          ListElement&lt;T&gt; firstElement = new ListElement&lt;&gt;(element);
          if (this.size == 0){
              this.first = firstElement;
              this.last = firstElement;
          }
          else{
              firstElement.next = this.first;
              this.first = firstElement;
          }
          this.size ++;
      }
  
      /**
       * Inserts the given element at the end of this list.
       *
       * @param element An element to insert into the list.
       */
      public void addLast(T element) {
          ListElement&lt;T&gt; lastElement = new ListElement&lt;&gt;(element);
          if(this.size ==0){
              this.first = lastElement;
          }
          else{
              this.last.next = lastElement;
          }
          this.last = lastElement;
          this.size ++;
      }
  
      /**
       * @return The head of the list.
       * @throws NoSuchElementException if the list is empty.
       */
      public T getFirst() {
          if (this.first != null){
              return this.first.data;
          }
          else{
              throw new NoSuchElementException();
          }
      }
  
      /**
       * @return The tail of the list.
       * @throws NoSuchElementException if the list is empty.
       */
      public T getLast() {
          if(this.last != null){
              return this.last.data;
          }
          else{
              throw new NoSuchElementException();
          }
      }
  
      /**
       * Returns an element from a specified index.
       *
       * @param index A list index.
       * @return The element at the specified index.
       * @throws IndexOutOfBoundsException if the index is out of bounds.
       */
      public T get(int index) {
          if(index &lt; 0|| index &gt;= this.size){
              throw new IndexOutOfBoundsException();
          }
          else{
              ListElement&lt;T&gt;element = this.first;
              for(int i = 0; i &lt; index; i++){
                  element = element.next;
              }
              return element.data;
          }
      }
  
      /**
       * Removes the first element from the list.
       *
       * @return The removed element.
       * @throws NoSuchElementException if the list is empty.
       */
      public T removeFirst() {
          if(this.first != null || this.size != 0){
              ListElement&lt;T&gt; list = this.first;
              this.first = first.next;
              size --;
              if(size() == 0){
                  last = null;
              }
              return list.data;
          }
          else{
              throw new NoSuchElementException();
          }
      }
  
      /**
       * Removes all of the elements from the list.
       */
      public void clear() {
          this.first = null;
          this.last = null;
          this.size =0;
      }
  
      /**
       * Adds the element to the top of the stock.
       * @param elem
       */
      @Override
      public void push(T elem) {
          ListElement &lt;T&gt; list = new ListElement&lt;&gt;(elem);
          if( first == null){
              first = list;
              last = first;
          } else{
              list.next = first;
              first = list;
          }
          size ++;
      }
  
      /**
       * Removes and returns the top element in stack,
       * that is the element that was last added.
       * Throws an EmptyStackException if stack is empty.
       * @return the top element in the stack.
       */
      @Override
      public T pop(){
          if(isEmpty()){
              throw new EmptyStackException();
          }else{
              ListElement &lt;T&gt; list = first;
              first = first.next;
              size --;
  
              return list.data;
          }
      }
  
      /**
       * returns the top element in the stack without removing it.
       * Throws an EmptyStackException if stack is empty.
       * @return the top element.
       */
      @Override
      public T top() {
          if(isEmpty()){
              throw new EmptyStackException();
          }else{
              return first.data;
          }
      }
  
      /**
       * Returns the number of elements in the stock
       * @return The number of elements in the stock.
       */
      public int size() {
  
          return this.size;
      }
  
      /**
       * Note that by definition, the list is empty if both first and last
       * are null, regardless of what value the size field holds (it should
       * be 0, otherwise something is wrong).
       *
       * @return &lt;code&gt;true&lt;/code&gt; if this list contains no elements.
       */
      public boolean isEmpty() {
  
          return first == null &amp;&amp; last == null;
      }
  
      /**
       * Creates a string representation of this list. The string
       * representation consists of a list of the elements enclosed in
       * square brackets (&quot;[]&quot;). Adjacent elements are separated by the
       * characters &quot;, &quot; (comma and space). Elements are converted to
       * strings by the method toString() inherited from Object.
       *
       * Examples:
       *  &quot;[1, 4, 2, 3, 44]&quot;
       *  &quot;[]&quot;
       *
       * @return A string representing the list.
       */
      public String toString() {
          ListElement&lt;T&gt; listOfElements = this.first;
          String returnString = &quot;[&quot;;
          while(listOfElements != null) {
              returnString += listOfElements.data;
              if(listOfElements.next != null){
                  returnString += &quot;, &quot;;
              }
              listOfElements = listOfElements.next;
          }
          returnString += &quot;]&quot;;
          return returnString;
      }
  }
  
  </code></pre>
  <pre><code>import org.junit.Test;
  import org.junit.Before;
  import org.junit.Rule;
  import org.junit.rules.Timeout;
  import static org.junit.Assert.*;
  import static org.hamcrest.MatcherAssert.assertThat;
  import static org.hamcrest.CoreMatchers.*;
  import java.lang.Integer;
  import java.util.EmptyStackException;
  import java.util.stream.IntStream;
  
  /**
   * Abstract test class for Stack implementations.
   *
   * Implementing test classes must only implement the getIntegerStack
   * method. Be careful not to override ANY other methods!
   *
   */
  public abstract class StackTest{
      @Rule public Timeout globalTimeout = Timeout.seconds(5);
  
      private Stack&lt;Integer&gt; stack;
      private int[] valuesInStack;
      private int initialStackSize;
      private Stack&lt;Integer&gt; emptyStack;
  
      @Before
      public void setUp() {
          valuesInStack = new int[] {3, 4, 1, -123, 4, 1};
          initialStackSize = valuesInStack.length;
          stack = getIntegerStack();
          pushArrayToStack(valuesInStack, stack);
          emptyStack = getIntegerStack();
      }
  
      /**
       * Push an array to the stack, in order.
       *
       * @param array An int array.
       * @param stack A Stack.
       */
      private void pushArrayToStack(int[] array, Stack&lt;Integer&gt; stack) {
          for (int i = 0; i &lt; array.length; i++) {
              stack.push(array[i]);
          }
      }
  
      /**
       * This is the only method that implementing classes need to override.
       *
       * @return An instance of Stack.
       */
      protected abstract Stack&lt;Integer&gt; getIntegerStack();
  
      @Test
      public void topIsLastPushedValue() {
          // Arrange
          int value = 1338;
  
          // Act
          emptyStack.push(value);
          stack.push(value);
  
          int emptyStackTop = emptyStack.top();
          int stackTop = stack.top();
  
          // Assert
         assertThat(emptyStackTop, equalTo(value));
       assertThat(stackTop, equalTo(value));
      }
  
      // HELPERS
  
      /**
       * Pops the desired amount of elements.
       *
       * @param stack A Stack.
       * @param amountOfElements The amount of elements to pop.
       */
      private void popElements(Stack&lt;Integer&gt; stack, int amountOfElements) {
          for (int i = 0; i &lt; amountOfElements; i++) {
              stack.pop();
          }
      }
  
      /**
       * Class used for stream operations when both actual and expected values
       * need to be gather in conjunction.
       */
      private class ResultPair&lt;T&gt; {
          public final T actual;
          public final T expected;
  
          public ResultPair(T actual, T expected) {
              this.actual = actual;
              this.expected = expected;
          }
      }
  }
  
  </code></pre>