I am a bit confused of that problem currently. I noticed that ArrayList is a class which implements List, which in turn extends Collection.
In Collection<?>
we have a bunch of methods and one recognizable is containsAll()
. I looked at the official documentation for ArrayList
but what can be seen there is that Array list has no such a method. There is something written in the footer of the page, saying "inherited methods" and containsAll()
is mentioned there.
What I don't understand (from the documentation) is, is the containsAll()
defined even if it's with an empty body in the ArrayList class or not at all? If not is this some sort of violation of the rules of Java?
There are others "missing" in the same way?!
containsAll()
method is defined by AbstractCollection
which is extended by AbstractList
, which is in turn extended by ArrayList
. So ArrayList
inherits containsAll()
implementation.
Consider the following code:
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
boolean contains = list.containsAll(Arrays.asList("b", "c"));
Here, when list.containsAll()
is called, actually the method declared in AbstractCollection
is executed.