3

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;
}
Nederes
  • 125
  • 1
  • 10

1 Answers1

3

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

Bathsheba
  • 231,907
  • 34
  • 361
  • 483