S
S
syntax2014-02-02 10:25:21
Java
syntax, 2014-02-02 10:25:21

What is the purpose of interfaces in programming?

It's not entirely clear what exactly interfaces are for. What for to me in one place to describe methods, and in another to implement? Why exactly do I need an extra space for the description, if I can do it right in the class? Or is it done to make the code easier to read? For example, if I see that a class is inherited from an interface, do I know what functionality it has? If yes, then why not come up with a class name from which the functionality will be clear? What comes first - interface or class? If possible, provide code examples (preferably not from the object area, preferably in Java) that show that in this situation it is necessary to use an interface, or that using it is more convenient than not using it. Books give examples everywhere of what an interface can do, but in the examples from the book, I do not see the urgency of using it. Thank you!

Answer the question

In order to leave comments, you need to log in

10 answer(s)
Z
ZzZero, 2014-02-05
@syntax

I'm making a brightness control system.
I want to adjust the brightness of everything (garlands, chandeliers, flashlight, phone screen).
In code it looks like this

class BrightControl
   public void setDefaultBright(Object obj){
         obj.setBright(10);
   }
}

The setDefaultBright method accepts any object. After all, I don't care what brightness to adjust.
My code is used by other developers, I have no control over them.
How can I make sure that the object that was given to me as an argument has a setBright method?
I'm writing an interface, and I'm saying that the setDefaultBright method only accepts objects that implement that interface.
If, apart from myself, no one will use this brightness control system. Then I’ll just keep in mind that only objects that have a setBright method can be sent to the setDefaultBright method, but code support becomes more complicated, you won’t remember in a year ...

D
Dan Ivanov, 2014-02-02
@ptchol

An interface is actually a regulation of interaction.
A class that implements an interface must implement all of its methods.
In the interface, you describe only method signatures, that is, you specify what the successor class should be able to do, but how it will do it, it decides for itself.
Thus, you are sure that if a class implements one or another interface, all objects of this class have a certain set of methods.
OOP is a world of abstractions :) Let it in :) Interfaces are yet another abstraction that allows you to separate description from implementation.
"Come up with a class with the right name" - so you can not force the "heirs" to implement the functionality.
Interfaces sit one level above classes, so to speak. They implicitly "combine" classes that are similar in some common way, and which are required (according to the logic of your application) to implement certain methods.

interface Instruments {
    final static String key = "До мажор";
    public void play();
}
class Drum implements Instruments {
    public void play() {
        System.out.println("бум бац бац бум бац бац");
    }
}
class Guitar implements Instruments {
    public void play() {
        System.out.println("до ми соль до ре до");
    }
}

ps: programmers will add and correct.

R
rEAcT1oNmanT1s, 2014-02-02
@rEAcT1oNmanT1s

Yes, this is done specifically for human perception of the code. Light programs, where there are not very many lines of code, are not very difficult for human perception, and programs that have thousands of lines of code should be divided into files accordingly, in some files there are conditions "what needs to be done?", and in others "how to do it" ?".
There is also the principle of modularity which says that a program can be divided into modules or blocks. The block contains the header files and the executable file that I mentioned earlier.
If the program is huge and you know about it in advance, then it should be divided into modules in advance. Why or why? The first reason is the human readability of the code, which has already been discussed, and the second will be much easier to find errors in the source code that you will write and they will be.
PS Even when writing something or asking questions, you can divide it into blocks of text, as I wrote, it is easier to perceive than what is written in a heap, isn't it?

L
leventov, 2014-02-02
@leventov

OOP#Basic Concepts
Without an interface (as a concept), three of the four basic concepts of OOP are inconceivable.
Specifically, in Java, interfaces as separate entities are needed because there is no multiple inheritance. In C++ and many other languages ​​with support for multiple inheritance of interfaces, there is no separate entity (a purely virtual class is a special case of an ordinary abstract class).

A
Alexey Pomogaev, 2014-02-02
@Foror

First you need to immediately understand that an interface is a special case of a class. But in Java it has a separate keyword, while in C++ it's just a class with no implementation. Therefore, the interface simply sets a certain standard for working with a bunch of different implementations.
For example, the Iterable interface says that classes that implement this interface have elements and can be iterated over in a loop by calling the next() method. So, if you create some kind of container or collection, then you can implement Iterable. Without inventing your own methods for working with the container. Thus, a certain common standard appears - an interface for working with containers.
And if you are making a game, you can create the Unit interface, thereby giving the classes certain behavior. For example, a unit must necessarily have an atack(), isDead() method, etc.
And then, in a loop, you check all units:
loop(...) {
if (unit.isDead())
removeFromMap(unit);
}
And of course, Unit can be just a class or an abstract class in which atack and isDead are implemented, or it can only be isDead, because attack is individual for each type of unit and requires its own implementation. Those. we come to the conclusion that an interface is also a special case of an abstract class.
Those. here polyformism already comes into play, i.e. interfaces essentially give polymorphism. Well, in Java, they also allow you to do multiple inheritance, or in other words, set several behavior properties to the class, for example, Unit can also be Iterable, thus you can give units an inventory and iterate over the elements in it.
And accordingly, if you have Unit as a class or an abstract class, then by inheriting Unit in Java, you simply cannot give the child Iterable behavior if Iterable is also a class.
OrcWarrior implements Unit, Iterable - this is how you can
OrcWarrior extends Unit, Iterable - this is not possible in Java, but it is possible in C ++, and Unit and Iterable will then always be declared as a class ...
Because of this, not inheritance is welcomed in Java, and composition. Those. What for each time to implement Unit.isDead if it is standard? Therefore, let's say a UnitAI class is created and the following is done:
class OrcWarrior implements Unit, Iterable {
UnitAI ai;
UnitAI getAI(){
return ai;
}
}
class UnitAI {
boolean isDead() {
....
}
}
interface Unit {
void attack();
UnitAI getAI();
}
This is called composition, ie. in OrcWarrior, HumanWarrior, you mix in UnitAI, which already implements isDead, and thus you do not need to implement it in the same code every time. In C++, this can be avoided, there is support for multiple inheritance, but it has its drawbacks. However, as well as the composition has pluses/minuses.

A
Alexander Pershin, 2014-02-02
@AlexPershin

The interface is the thing that makes it possible for developers to "communicate" very effectively. Especially in large projects with a huge number of developers. You don't have to run around and explain to every one of a hundred people how to use your class. You think over a good and understandable interface - and they use it. You can generally leave the company, and the interface will live and be used.
And if you look at open frameworks, like Zend, then there are no interfaces at all.
In general, think of the interface as the most effective communication tool for developers on a team.

Q
Quber, 2014-02-03
@Quber

@syntax I'll be short. You understand everything correctly. You can describe an interface in a class, but it is considered more convenient with an interface. Pros are in other comments. However, if there is no need, you can not write interfaces, this is everyone's business.

S
svd71, 2014-02-03
@svd71

Classes allow you to expand child elements only in the functionality of classes. Interfaces also allow you to extend functionality, but for any class. This is the main difference : Classes can be extended "vertically" (each child can extend a class), interfaces allow classes to be extended "horizontally" (an interface can be implemented in any class, even if they do not have common ancestors).

O
OnYourLips, 2014-02-02
@OnYourLips

An interface is what a class looks like to the outside. Using an object by interface, you don't have to think about what class it is.
An example is template-author in Symfony 2.

P
pazukdev, 2021-01-10
@pazukdev

1. A class is always an interface + an implementation (at least a partial one).
2. An interface is a way to completely separate the interface from the implementation. Those. a completely abstract class. Used to describe the contract behavior of a class for interacting with the external environment.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question