Answer the question
In order to leave comments, you need to log in
What are interfaces for anyway?
I can’t understand what interfaces are for and how to use them correctly ?! On the Internet it is written in order to use one method described in the interface (which fits two classes).
Why can't this method be created in the same way in different classes??
Answer the question
In order to leave comments, you need to log in
Imagine you have some method that takes in arguments an object, for example, of type User.
You can pass any object of the User type or objects of classes that inherit User into this method.
But you can make it even more flexible, for example, describe a certain UserInteface interface and specify it in the method definition:
Now you can pass an object of any class that implements the UserInterface interface to the method.
Of course, the original User class in this example must also implement this interface.
That is, by specifying a specific class in the parameter data type, you bind to this class or its descendants, and by specifying an interface, you bind to this interface, which any other class can implement, the cohesion decreases, which is good. And if you simply write the necessary methods in these not yet written classes, then you will not be able to use the object of these classes, the data type will be different, although the methods will be described.
And, as already written above, one class can implement several interfaces.
Repairing a submarine: screw fastening - 16-leaf thread with 29 inches ( just invented ). No need to make a new submarine every time - you need to replace the propeller according to the specifications. What screw will be - implementation. The necessary parameters for the screw to fit - and there is an interface.
A more complex example: an interface adds properties-behavior to a class, but does not implement it. The wheel can spin, but to change the nipple, you need to implement the tire fitting protocol: it would have a pointer to the nipple, diameter, type of rubber, etc. There are many wheels at the tire fitting, but provided that the wheel has implemented the protocol, the masters will be able to help. But they won’t help with plastic wheels - there is no nipple, the protocol cannot be implemented.
In fact, everything is quite simple: interfaces are needed to decompose the model and facilitate software development. In fact, an interface describes a set of operations that can be performed on an object. If you write some kind of functionality, then let's say it works only with those objects that you can perform certain actions on, and you don't care what these objects are. The letter "I" from the set of principles SOLID about this, read.
Let's say I'm writing a module that should knock on everything that can be knocked on. I have the "knock(IKnockable o)" function and the corresponding interface. Clients who need to use this module implement this interface in their classes and use it. But my module does not know anything about clients at all, it just works with everything that implements a certain interaction protocol (interface). It also reduces connectivity.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question