I’m a bad storyteller if I don’t have the goal of explaining something incomprehensible to someone with a specific request, so I understand the simple things, and for one thing I’ll write about it here. I don't follow the JavaRush teaching example of first asking a question and then telling you how to do it. I am a supporter of first telling, and then asking, as with telling, first showing, and then explaining.
BS: Actions == behavior, take them as synonyms, actions are simply more correctly understood, it is always something active, and behavior may not show anything.
interface - interface
What is this? Example interface listing (with generics):public interface Store<T, ID> {
void add(T t);
void update(T t, ID i);
void delete(ID i);
T findOne(ID i);
List<T> findAll();
}
Common crud (from CRUD: Create, Read, Update, Delete) interface. Don't be alarmed, everything is quite simple. This interface means that working with many databases is based on the following principle: you need to add a record, update a record, delete and find all records. You will see this 100 more times and you will write almost the same interface the same number of times. It is designated public because it must be implemented by the class. What does it mean to be implemented by a class ? Everything is very simple: it must describe all the methods of the interface. The simplest example of implementation in a class:
void add (T t) {
bookstore.add(t);
}
Instead of T t, a certain generic will be substituted. In a nutshell, this is the TYPE of a variable, which is why it is most often denoted as T = Type. In our example, let's say it could be like this:
void add (Book book) {
bookstore.add(book);
}
Naturally, in place of Book book you can substitute any variable that you need for your implementation. To understand, you can theoretically imagine that T = Object, and since in Java everything is Object, you can also define any of your classes/variables there. The only trouble will arise later - exceptions if the class is zero, etc. In its own words, it is a certain program structure that defines the relationships between objects by dividing them into certain behavioral parts. The purpose of an interface is to define functionality for implementation by a class. That is, a description of method signatures. Inside the interface there are names of methods that return and input values, and that’s it. Well, you can still designate some variables there, but they immediately become implicitly public static final, and can be accessed from any part of the program. The interface describes actions, and therefore these same actions are contained in it, i.e. functions or methods. Why is this necessary? What does this simplify or what advantages does it provide? Simplifying the description of actions and behavior. We say what to do, but each class implements how to do it itself. Save time on large projects. Interfaces are created in situations where we know we need to perform some task, but how to do it may vary. The interface describes the names of the actions - this is simply the direction of some abstraction. Although there may also be interfaces without methods and fields, such as markers such as Cloneable, Remote, etc. Let's take everyone's favorite example of a car. The interface in it will describe the possible actions of the car, turning the steering wheel or direction of movement, speed gain, remaining gasoline, etc. That is, the same actions that absolutely any car can have. In other words, we descend into the jungle of degradation to the lowest level of creation of the very first car and with our own brains we figure out how it was created and what it had. Naturally, we describe this in the abstract and only for actions. What did the very first car have? Was there a steering wheel? It was, which means it was turning somewhere, the direction of the steering wheel/movement. Were there wheels? Yes, that means I was driving at some speed, a change in speed. That's the whole interface. But in general, interfaces are created for some implementation of certain actions. Those. we write a program for more specific ones than for anything else you can think of. Therefore, the interfaces themselves will contain clearer and more specific methods. Of course they will be as abstract as possible. Interfaces can be inherited from each other as classes.
interface MyInterface extends NotMyinterface;
Interfaces are implemented in classes. You can implement as many interfaces as you like. Unlike inheritance, inherit from only one.
class NewClass extends OldClass implements MyInterface, NotMyinterface;
Those. we came up with some actions, gave them names, input data, return data, wrote all this in the interface, then created a class and added our interface to this class, i.e. implemented our interface in this class. Further, all methods/functions described in the interface must have an implementation. It can be done directly in the interface itself by adding the word default before the method and writing the implementation directly in the method, like a class. This became possible with version 8 of Java. It can also be done in a class that will implement this interface. Well, we’ve written the interface, implemented it in the class, rolled out the implementation in the class, we can run it and test it. You can also read in the interfaces about:
- Static methods.
- Private methods. (code repeat)
- Variables/Constants.
- Nested interfaces.
line1: Volvo v = new VolvoV2();
line2: Volvo v = new VolvoV3();
In addition to interfaces, it is worth continuing about Abstract Classes and Classes. Maybe later, when I have a couple more hours free. ))) PS: Guys, I ask for all criticism under the post or in PM, I perfectly understand that everyone has it))) and I’m always interested in hearing it, because this is a reason to become a little better and again continue to move forward. With that, thank you all so much and good luck in programming. )))
GO TO FULL VERSION