Good day to all.

The task to understand the interfaces. I have an interface

interface ICheturehugolMain { int x { get; set; } int y { get; set; } int width { get; set; } int hight { get; set; } void Draw(Graphics g); } 

And also 3 classes inherited from this interface, say, one of them

 class CheturehugolVrashatelnoe : ICheturehugolMain { public int x { get; set; } public int y { get; set; } public int width { get; set; } public int hight { get; set; } public CheturehugolVrashatelnoe() { } public void Draw(Graphics g) { } } 

So, we used the interface, but I just can not understand where the logic of its use, it turns out, created a template (interface) (method and 4 properties) and in 3 classes implemented them in our own way, with the same success it was possible just create 3 classes and do not make any interface, or create an abstract class and redefine where necessary the methods in the classes of the heir (it is clear why and why), but still I want to know how to correctly implement the interface in this task so that it carried some meaning in the code.

Thank you in advance.

  • one
    The interface is just needed to create a certain "template" that binds classes to the heirs to provide certain methods. For example, if you take the interface "car", and in it the method "start the engine", then from this it follows that for any car you can "start the engine". And how it will be done depends on the specific implementation: press the button, like on a sports car, turn the key, like on most, or push it with men, like on an old Muscovite. The abstract class is not an option, as in C # there is no multiple inheritance. - Valeriy Karchov

2 answers 2

You can create an abstract class, but the main difference between classes and interfaces is that the class can implement as many interfaces as possible, and its base class is always the same.

  • In this task, when we have one interface, it’s better to make an abstract class after all (or how to scatter 4 properties and one method across different interfaces, (well, to use the interfaces anyway)), and if we would have many interfaces, then there is no way to do without them (due to the possibility of implementation). - AndereyMaybe
  • Yes, and by the way, an abstract class is created when there is concrete or general logic for posterity. and if you just create an abstract class with abstract methods, then the interface is better. - Valeriy Karchov

The main difference between a class and an interface is that a class consists of an interface and an implementation.

Any class always implicitly declares its interface - what is available when using the class from the outside. You have an ICheturehugolMain interface that describes the behavior of objects.

Suppose you have a method that draws objects. Then you can declare it like this:

 void DrawCheturehugol(ICheturehugolMain figure) { figure.Draw(); ... } 

or so:

 void DrawCheturehugols(IEnumerable<ICheturehugolMain> figures) { foreach (ICheturehugolMain figure in figures) { figure.Draw(); ... } } 

What does this give us?

  1. All figures can have their own implementation of the Draw method.
  2. The code does not depend on specific classes. It depends on the interfaces.
  3. You can combine these shapes in a collection. For example:

List, ICheturehugolMain [], etc.