Search code examples
javainheritancearraylistis-empty

Implementation of isEmpty()


I'm trying to figure some basic things out. I was exploring standard library ArrayList.java when found that ArrayList has implementation of method isEmpty().

ArrayList.java:

public boolean isEmpty() {
    return size == 0;
}

ArrayList extends AbstractList extends AbstractCollection. And AbstractCollection has implementation of isEmpty as well:

public boolean isEmpty() {
    return size() == 0;
}

I'm just trying to get the logic? Why ArrayList implements already implemented method? What for?

P.S. ArrayList also has size

public int size() {
    return size;
}

Solution

  • The ArrayList version is a minor, but effective, optimisation.