An abstract
class is a class that implements most of the functionality of an object but leaves critical functionality for you to implement.
When it is encouraged (or even expected) that implementations of an interface will be created it is common to provide all possible assistance to do that. This is where the Abstract...
classes are extremely helpful. Not only do they implement the interface but they expose a minimal set of abstract
methods that you must implement to achieve that.
Consider this implementation of a list that contains one single String
using AbstractList
:
class OneHelloList extends AbstractList<String> implements List<String> {
@Override
public int size() {
return 1;
}
@Override
public String get(int index) {
return "Hello";
}
}
public void test() {
for (String s : new OneHelloList()) {
System.out.println(s);
}
}
See how simple it is for me to achieve the full functionality of a List
with a very small code base.
Contrast that with what I would have to do to implement List
without the AbstractList
:
class TwoHellosList implements List<String> {
@Override
public int size() {
return 2;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean contains(Object o) {
return o.toString().equals("Hello");
}
@Override
public Iterator<String> iterator() {
return new Iterator<String>() {
int count = 0;
@Override
public boolean hasNext() {
return count < 2;
}
@Override
public String next() {
count += 1;
return "Hello";
}
};
}
// The rest for you to complete.
@Override
public Object[] toArray() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public <T> T[] toArray(T[] a) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean add(String e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean containsAll(Collection<?> c) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean addAll(Collection<? extends String> c) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean addAll(int index, Collection<? extends String> c) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void clear() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public String get(int index) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public String set(int index, String element) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void add(int index, String element) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public String remove(int index) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public int indexOf(Object o) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public int lastIndexOf(Object o) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public ListIterator<String> listIterator() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public ListIterator<String> listIterator(int index) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public List<String> subList(int fromIndex, int toIndex) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}