Why in Java, besides collection interfaces, there are abstract classes corresponding to them. For example, there is a Collection
interface. and abstract classes AbstractCollection
, List
and AbstractList
. Why is interface alone not enough?
3 answers
Abstract classes are helper classes that provide a basic default "skeleton" implementation of the functionality of the corresponding interfaces. For example, the AbstractCollection
class provides the user with a “skeleton” implementation of the Collection
interface. In order to implement your collection, you just need to inherit from AbstractCollection
and provide your own implementations of those methods that are specific to your collection. And those that are non-specific are simply inherited from the AbstractCollection
. Thus, you will not need to write a bunch of "boring" methods with a trivial or routine implementation.
At the same time, you are not required to use the AbstractCollection
. You have every right, if you so wish, to implement the Collection
interface yourself, from scratch, without using the services of AbstractCollection
.
Likewise, a division has been made between List
and AbstractList
.
- Does this correspond to any design pattern? - jisecayeyo
- It's not entirely clear why, for example, the ArrayList class inherits AbstractList and implements the List interface? - jisecayeyo
- one@jisecayeyo: But I don’t understand what can be incomprehensible here. The purpose of
ArrayList
precisely to implement an array accessible via theList
interface. In full accordance with my answer, the authors ofArrayList
, in order to reduce the amount of routine work, used the "skeletal" implementation provided byAbstractList
. - AnT
Simply say:
interface : it is a characteristic of a class, whereby there can be many characteristics of an abstract ancestor class : it is the core of a class, often a strategy of behavior
That is, things are very different - it is not surprising that for the same class there are both concepts.
- Then it is not entirely clear why, for example, the ArrayList class inherits AbstractList and implements the List interface? - jisecayeyo
- @jisecayeyo Well in human terms, the List is his characteristic, it is immediately clear that this is a list. AbstractList is a list behavior strategy. And ArrayList is a class using this strategy and expanding behavior is not just a list, but an array list. The need for interfaces is (IMHO) - in that you immediately see what characteristics a class has - you can find classes with similar characteristics, see where you use. Those. This is necessary not only for polymorphism to work - the readability and human-likeness of code in large projects is paramount. - Goncharov Alexander
Interface
- shows that the class object has some behavior, but without specifying how it does it
Abstract class
- the idea is the same, but in it, as a rule, partial implementation is already present. This allows you to get rid of duplication of code in the descendant classes. With the abstract class
, the design template method
pattern template method
widely used. I will give an example from the same java.util.AbstractList
. There is a method addAll(int index, Collection<? extends E> c)
having a concrete implementation, it calls add(int index, E element)
, which in turn is already abstract.
ArrayList
inherited fromAbstractList
'a, then what's the point of appending theimplements List
? In fact, it does not affect the functionality (Checked by analogy with its program), except for the case when the abstract class implements more than one interface. And so, apparently, this was done to please the readability of the code. However, I can not take something into account. - I. Perevoz