Answer the question
In order to leave comments, you need to log in
The meaning of the interface (not GUI) and why is it needed at all?
Hello!
I read a couple of topics, and as a result, based on what I read, I concluded that the interface is needed for a kind of mandatory class framework that implements this same interface.
Those. creating a class and implementing an interface, the developer must define the fields/properties/methods of the interface in the class.
But I'm wondering if this solution is worth it. What prevents us initially at the stage of class planning from including certain fields / properties / methods in them without resorting to interfaces?
Only one variant of expediency comes to mind, that development is carried out by a group of people, and just like that, the interface obliges the designers of various classes to adhere to a certain standard in the class structure.
Those. I can conclude that the interface, in essence, is intended rather for convenience and organization in the structures of various classes, i.e. to give them something in common, but implemented in each in its own way, at the same time, to oblige to implement exactly everything that is indicated in the interface.
I hope the question is clear, and maybe I answered it myself, but still, I want to be sure that my conclusions are correct so that in the future I don’t get into an absurd situation.
If my arguments are wrong, I ask you to correctly explain the meaning and scope of interfaces.
For example, there is an interface that describes weapons (damage, damage type, ammo volume, ammo type), and there are classes that describe different types of weapons, but at the same time they must have this interface for their own description.
Thank you!
Answer the question
In order to leave comments, you need to log in
The interface is another level of abstraction. This is more advanced programming at the level of what to do, not how to do it.
How to use the library? How to link two programs, two different pieces when? How to build flexibility into a project? How to foresee the modernization of the program? You have to use the interface.
One common use case is to use an interface as a generic data type for different classes.
For example, there is an interface "Shape", which has two methods - "calculate area" and "calculate perimeter".
There are classes that implement this interface - square, circle, triangle, trapezoid.
And somewhere you need to store something like "current figure" in meaning - so you can use "Figure" as a data type.
I'm not ready to give an assessment of your understanding of OOP ...
but along the way, it's time to tumble in the functional paradigm ... (it definitely won't get worse)
ps interface - an alternative to multiple inheritance. something like this
A simple example. You have some kind of program that should send a report. Today you have implemented a class with saving to a file, and tomorrow you will need to send this report in a different way. And each time you will write a new class rewriting the logic of the program? What for. When an interface is created and when passing the report somewhere, the interface method will be used. But how it will be implemented is the tenth thing. The main thing is that the method for transferring the necessary data will be clearly defined at the expense of the interface.
An interface is a clear set of methods for an implemented class or structure that is required in a program. And it's not about the programming team.
Here is one of the patterns in which interfaces are used, where a specific method is called and a completely different part of the program is responsible for its implementation.
Put + many, and add.
Read about SOLID , letter D.
You don't have to do anything just because others do it. But you want to understand this if you want to write not home pet projects, but slightly more complex software that many people will support for years.
Inversion and polymorphism is a very important topic in software development and OOP. Many patterns, principles are tied to this.
GOF, SOLID, GRASP.
You can't even write tests without interfaces.
All this was not invented to fool your head, it is the result of the work of many programmers who have been coming to them on their own for decades.
Those. I can conclude that the interface, in essence, is intended rather for convenience and organization in the structures of various classes, i.e. to give them something in common, but implemented in each in its own way, at the same time, to oblige to implement exactly everything that is indicated in the interface.
Those. I can conclude that the interface, in essence, is intended rather for convenience and organization in the structures of various classes, i.e. to give them something in common, but implemented in each in its own way, at the same time, to oblige to implement exactly everything that is indicated in the interface.
I hope the question is clear, and maybe I answered it myself, but still, I want to be sure that my conclusions are correct so that in the future I don’t get into an absurd situation.
If my arguments are wrong, I ask you to correctly explain the meaning and scope of interfaces.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question