Skip to content

Instantly share code, notes, and snippets.

@invasionofsmallcubes
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save invasionofsmallcubes/1bc6787596f6bfb6370f to your computer and use it in GitHub Desktop.
Save invasionofsmallcubes/1bc6787596f6bfb6370f to your computer and use it in GitHub Desktop.
usage of abstract class instead of implementation on interfaces
// In Java 8 we have
public interface List<E> extends Collection<E> {
default void sort(Comparator<? super E> c)
{
Collections.sort(this, c);
}
}
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable{}
// What I would do in general.
// Generally you just add an abstract level, right?
public interface List<E> extends Collection<E> {
void sort(Comparator<? super E> c);
}
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
public void sort(Comparator<? super E> c){
Collections.sort(this, c);
}
}
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable{}
// Basically you have two responsibilites associated to the interface:
// Declaring a contract and implementing a part of it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment