Last active
August 29, 2015 14:11
-
-
Save invasionofsmallcubes/1bc6787596f6bfb6370f to your computer and use it in GitHub Desktop.
usage of abstract class instead of implementation on interfaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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