Que sont les classes et les interfaces en C# ?

Interfaces are to make an abstraction, an archetype, of the abstraction, the classes, of the reality, the objects.

Interfaces are to specify contract terms without providing implementation provided by classes.

Interfaces are specifications:

  • Interfaces are design time artifacts to specify the immobile behavior of the concept as it is alone and static.
  • Classes are implementation time artifacts to specify the mobile structure of the reality as it interacts and move.

What is an interface?

When we observe a cat we can say that it is an animal that has four paws, a head, a trunk, a tail and hair. We can see that he can walk, run, eat and meow. And so on.

We have just defined an interface with its properties and its operations. As such we have not defined any modus operandi, but only features and capabilities without knowing how things work: we have defined abilities and distinctions.

As such it is not really yet a class even though in UML we call this a class in a class diagram because we can define privates and protected members to begin having a deep view of the artifact. Do not be confused here because in UML an interface is a slightly different thing that an interface in C#: it is like a partial access point to the abstraction atom. As such we said that a class can implements multiple interfaces. As such it is the same thing, but not, because interfaces in C# are both used to abstract the abstraction and to limit this abstraction as an access point. It's two different uses. Thus a class in UML represents a full coupling interface to a programming class, whereas an UML interface represents a decoupling interface of a section of a programming class. Indeed, the class diagram in UML does not take care of the implementation and all its artifacts are at the programming interface level. While we map UML classes to programming classes, it is a transposition of abstract abstraction into concrete abstraction. There is a subtlety that explains the dichotomy between the field of design and the field of programming. So a class in UML is a programming class from the point of view of a programming interface while considering inner hidden things.

Interfaces also allow to simulate multiple inheritance when not available in an awkward way. For example, the cat class will implement the cat interface that derives itself from the animal interface. This cat class will also implement these interfaces: walk, run, eat and make a sound. This compensates for the absence of multiple inheritance at the class level, but each time we need to reimplement everything and we can not factor the reality at best like the reality itself do it.

To understand that we can refer to Pascal Object coding where we define in a unit the interface and the implementation sections. In the interface we define the types and in the implementation we implement the type:

unit UnitName;

interface

type
TheClass = class
public
procedure TheMethod;
end;

implementation

class procedure TheClass.TheMethod;
begin
end;

Here, the interface section match to the UML class design while Interfaces types are thus others things.

So in our business we have one word, interface, to nominate two distinct but similar things, and it is a source of confusion.

Also in C# for example, programming interfaces allow to compensate the absence of true generic polymorphism on open types without really succeeding the goal because we lost the strongly-typed hability.

After all, interfaces are necessary to allow incompatible systems to communicate without worrying about the implementation and the management of objects in memory like introduced with the (Distributed) Common Object Model.

What is a class?

After defining a reduction of the reality from an external point of view, we can then describe it from an inside perspective: this is the class where we define data processing and message management to allow the reality we have encapsulated to come to life and interact thanks to objects using instances.

Thus in UML we realize a fractal immersion in the wheels of the machinery and we describe the states, the interactions and so on to be be able to implement the abstraction of the fragment of the reality we want to handle.

As such, an abstract class is somehow the equivalent of an interface from the point of view of the compiler.

Links

CC BY-SA 4.0 Original Post