JavaRush /Java Blog /Random EN /What are interfaces
Level 18

What are interfaces

Published in the Random EN group
An interface is a public, generally accepted contract (agreement) that describes some behavior. What are they needed for? For example, let's say we have a link pointing to a list of strings. Let's say that many elements are added to this list at the beginning, and then the entire list is processed once. Let's say we decide to follow the well-known recommendation and use the classArrayList:
ArrayList<String> list = new ArrayList<>();
We wrote a program behavior in 100,500 lines of code that used this list of strings, and used class-specific methods to optimize the performance ArrayList. For example ensureCapacity() . As long as the lines are added to the end of the list, everything works fine and quickly. But now we have a need to reorient our program to a slightly different type of work, where lines are added mainly to the beginning of the list. For this type of load it is much more suitable LinkedList. But if we want to translate our program of 100,500 lines of code onto rails LinkedList, then we will need to find and remove the use of specific ArrayListmethods, perhaps in some places greatly changing the logic of individual sections of the program. If we used only those methods that are available in both ArrayList, and in LinkedList, then we would not have to do this. We could just change one line of code - the list declaration:
LinkedList<String> list = new LinkedList<>();
We can notice that it would be more convenient to place the declaration of common methods for these classes in an ancestor class, possibly abstract, for example AbstractList. In this case, we could declare our list like this:
AbstractList<String> list = new ArrayList<>();
And we could quickly switch the implementation like this:
AbstractList<String> list = new LinkedList<>();
But in this case, the classes that we can use in our program are limited to only the descendants of the class AbstractList, even if there are more suitable classes that are not descendants of the class AbstractList, but have the same methods with the same behavior. What should I do? This is why interfaces were invented . An interface is a convention about a set of methods and their behavior that completely unrelated classes can commit to, allowing any of them to be referenced by a single reference. For example like this:
List<String> list;
list = new ArrayList<>();
list = new LinkedList<>();
list = new AnotherListClass<>();
Even if AnotherListClassclasses do not have common ancestor classes, ArrayListexcept . A good example of an interface is the steering of a car - a car has a steering wheel, pedals and a gearbox. In the vast majority of cars, these elements follow the same behavior convention. For example, if you turn the steering wheel counterclockwise, the car will turn left rather than speed up, regardless of its make. If you know how to use these controls, you can easily handle any car, regardless of its model, year, make or engine type. Moreover, one can imagine a situation in which a completely different type of transport (for example, a spaceship) has the same control interface as cars. If you, knowing how to drive a car, find yourself in the pilot’s seat of such a ship, you will be able to avoid getting lost in this situation. Let's repeat once again:LinkedListObject
  • An interface is a contract (agreement) about behavior.
  • Many classes, even those not related by inheritance, can declare that they agree to abide by this contract (interface implementation).
  • Placing the description of behavior in a separate interface is very convenient, as it increases the flexibility of the code, allowing you to switch the implementation (class implementing the interface) of the interface to classes not related by inheritance.