Answer the question
In order to leave comments, you need to log in
OOP - How to understand Interfaces in java?
Interfaces, on the other hand, describe the structure that should be contained in classes or interfaces that are inherited from the current interface; they do not specify the implementation of a particular method.
So, for example, in order to add support for clicking on a button in android, one of the ways to implement it is to indicate that there is support for the ViewSetOnClickListener interface and add a listener, so what I can’t understand is why this is done?
What happens when interface support is added?
I seem to understand how interfaces work, but I don't seem to...
Answer the question
In order to leave comments, you need to log in
Look at the idea
, you create a "Button" object and this object can change its state (the yes/no button is pressed, this is the state). Other programmers in other projects will use your Button object, and those programmers want something to happen when the state changes. At the same time, you do not know what exactly should happen in all these projects (only their developers know this).
miserable banality: you make a method .getCurrentState()and other programmers in some loop interrogate your button through this method, and even store the previous state (you need to compare it with something). As you can see, it's pretty miserable... the user-programmer needs to write a bunch of code with checks and, in addition, waste memory and processor time (that is, the program lags and eats a lot of resources...).
normal implementation:let the button itself cause the necessary actions, it also knows when its state has changed. This means that the button should call some function, but at the same time not know what exactly this function should do. This is where interfaces and design patterns come to the rescue in this case, Listeners (aka Observers). That's just OnClickListener this is the interface. As for the button, it stores a list of objects that need to be notified when the state changes, the type of these objects is "OnClickListener". Thus, the "Button" does not care "what where and how" should happen when the state changes, but at the same time it provides the ability to perform this "what where and how" and at the same time guarantees that there will be no error due to the absence of the called method.
en.wikibooks.org/wiki/Java/Listeners
citforum.
Interfaces allow you to avoid multiple inheritance, roughly speaking. But, that's not the point. An interface is a group of methods that define some behavior. That is, if a class implements an interface - "fills" its methods with content, this means that it is endowed with some abilities. For example, if a class implements the Stack interface with methods push and pop, then it is endowed with the behavior of a stack (regardless of what these methods actually do, i.e. not necessarily objects of this class can be used as a stack). This allows you to abstract from the class, and start only from whether the class of a given object implements a certain interface or not.
An interface obliges the implementing class to adhere to some behavior.
If you implement an interface, then there is a guarantee that all methods described in the interface will be available to the user (the user is the programmer using the implementing class). And he does not need to get acquainted with the logic of the method, it is only important to know that the object definitely has this method.
This is not so much related to OOP, but to the architecture of the application as a whole.
I will give an example based on a simplified freelance exchange. There is a developer class, and there is a customer class. Each of them has its own interests and tasks. A developer can do some work and some not.
1. Therefore, he writes in his resume that, for example, he can develop applications for Android (the Android developer announces the interface).
2. On the exchange, he finds all the customers who previously gave orders under Android. He writes to them in a personal and says that if anything he is ready to consider their proposals and complete the work (signs as a listener).
3. Once the customer has an idea for an application. He pulls up his contact list and finds those who can develop for Android (that is, his "listeners"). And send messages to them.
The example is not quite complete, but I tried to show that the interface is a kind of contract. The class says that I would like to receive certain events from other classes and I have everything I need for this. The interface describes just what methods the class should contain in order to respond to the necessary events.
The interface indicates that the class meets all its requirements, or the program will not compile.
Any class can be used by the type of its interface. It doesn't matter what class it is.
Let's say you're choosing a new engine for your old car. To know that it will fit you need to know the required dimensions of the engine, the presence of all connectors, cables and other things that the car body requires. These requirements are the interface and it doesn’t matter if you put an engine from KAMAZ or something else there, as long as everything fits together as required. I hope I cleared things up a bit :)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question