Design/reiterate or code reuse for linked list This is what my class hierarchy looks like. I have an abstract superclass, AbstractLinkedMyList, that contains common operations for both sorted and unsorted linked list.

Here's the declaration for it and the method I have a question about:

public abstract class AbstractLinkedMyList&lt;Type&gt; implements MyList&lt;Type&gt; {
    protected ListNode&lt;Type&gt; front; 
     protected ListNode&lt;Type&gt; back;


public void insertAtIndex(int index, Type value) {
        checkOkToInsertAtIndex(index);
        if (size == 0) {
            ListNode&lt;Type&gt; newNode = new ListNode&lt;Type&gt;(value);
             front = back = newNode;
         }
        else {
            if (index == 0) {
                   ListNode&lt;Type&gt; newNode = new ListNode&lt;Type&gt;(value, front);
             front = newNode;
            }
           else {
                  ListNode&lt;Type&gt; current = nodeAt(index - 1);
                  ListNode&lt;Type&gt; newNode = new ListNode&lt;Type&gt;(value, current.next);
                 current.next = newNode;
                 if (index == size) {
                     back = newNode;
                 }
            }       
       }
   size++;
    }
     protected ListNode&lt;Type&gt; nodeAt(int index) {
         ListNode&lt;Type&gt; current = front;
         for (int i = 1; i &lt;= index; i++) {
                current = current.next;
          }
         return current;
    } 
  protected static class ListNode&lt;Type&gt; {
        public Type data;
        public ListNode&lt;Type&gt; next;  
        public ListNode(Type data) {
             this(data, null);
        }
        public ListNode(Type data, ListNode&lt;Type&gt; next) {
             this.data = data;
             this.next = next;
        }
     }
    ..... 
}


Here is my declaration for the sorted linked list:

public class LinkedListSorted&lt;E extends Comparable&lt;E&gt;&gt; extends AbstractLinkedMyList&lt;E&gt;


My question is about the insertAtIndex method. I chose to put the insertAtIndex method in the abstract superclass because this would be how you insert something at an index, no matter what the subclass is (sorted vs unsorted). I realized that in the sorted linked list class, I will have to extend this method to impose restrictions(to not break sorted property of the list).

This is my insertAtIndex method (what I have so far) in the sorted linked list:

public class LinkedListSorted&lt;E extends Comparable&lt;E&gt;&gt; extends AbstractLinkedMyList&lt;E&gt;
@Override
public void insertAtIndex(int index, E value) {
    if(index == 0 &amp;&amp; front != null &amp;&amp; value.compareTo(front.data) &gt; 0) {
        throwSortException();
    } 
    if(index == size &amp;&amp; value.compareTo(back.data) &lt; 0) {
        throwSortException();
    } 
    if(index &gt; 0 &amp;&amp; index &lt; size) {
        ListNode&lt;E&gt; before = nodeAt(index - 1);
        if(value.compareTo(before.data) &lt; 0 || value.compareTo(before.next.data) &gt; 0) {
            throwSortException();
        }
    }
    super.insertAtIndex(index, value);
}


My logic here is here are all the cases where you cannot insert (would break the sorted property of the list). If you can make it past all these cases, I will allow insertAtIndex, whose implementation is in the abstract superclass (explained is this decision above).

Would this be good code design? One drawback I saw right away was that my check logic involved a lot of iteration. You would need to this iteration again if you actually wanted to insert. But then if you put the insert logic in the check case, you would be rewriting code (not taking advantage of code reuse). How would you weight the two? Is what I have acceptable?
No, it would not be good design. That method signature says "you can insert an element at any point in the list" and then the body says "but only if it's the place I want you to put it." This is why the Collections API doesn't have a sorted list, but rather allows lists to be sorted. Sorted and Unsorted lists share some contract, but insertion is fundamentally different for them. Unless your assignment requires it, I would not put them in the same inheritance hierarchy, and I'd be leery of putting them under the same interface (contract) unless it didn't include addition of elements.