Gentlemen, it is possible, at least briefly to explain those. side of inheritance in c #. I mean exactly what happens with inheritance. Just copy the code from base to descendant, or create a base object inside the descendant with direct access to the public members of the base class, or some other option ?? Thank.

PS Google did not help, because different sites and video lessons contradict themselves. Who says that a descendant class takes the members of the base class as if to itself, who says that a descendant creates a base class object with the possibility of direct access;

  • 2
    > who does any magic at all, and can be an example of a source where inheritance is explained by magic? - DreamChild
  • Well, this I figuratively said))) Just in some sources, even in the same Schild there is a contradiction to itself. That's why I called this magic - Polyakov Sergey
  • @polyakov_s: The whole Shield is a contradiction to itself. Read normal books. - VladD

2 answers 2

For starters, you as a programmer need not worry about it. The specific implementation details are not as important, only how the code is executed is important.

In fact, to all appearances, (non-virtual) methods are created separately from objects, at compile time, and the ability of a class to call a method by name is determined only by the visibility tables of the compiler. In this case, the base class class tables are included by the compiler in the visibility tables of the derived class (in order to ensure the implementation of the standard).

By virtual methods, the picture is slightly different. When compiling a call to a virtual method, the compiler does not know which method will actually be called, so an additional level of indirection is introduced: a table of virtual methods. Each virtual method corresponds to an index in the table, and the real table depends on the actual type of the object (one per type), and is bound to the object during the execution of the constructor.

With data, everything is different: data inheritance means that the object instance requires memory for both its fields and ancestor class fields when created. This size, of course, is a constant for each given class, and is determined at compile time.

All this is basically just an example of a possible implementation. Since the standard does not require a specific implementation of inheritance, this implementation can be any. For example, it is theoretically possible to “attach” methods to an object by name in runtime (like ExpandoObject does).

    The only thing that is necessary to know here is that when inheriting a descendant class, all members of the base class with the exception of constructors (including static ones) and destructors get it. How exactly this is implemented in practice - the matter in general is not the tenth and of particular importance (it must be assumed that in various implementations of the language this can theoretically be done in various ways). If you pay attention to the specification of the language (paragraph 1.6.4), then nothing is said about the specific implementation of this mechanism, but only refers to the above:

    It means that it’s not worth it.

    • Thanks for the answer. But here another question appeared. It turns out that the keyword base. Member_name refers to a member in the base class or a member of the copy of the base class within the descendant ?? And did I understand correctly that when inheriting, a descendant takes the code from the base, and not the base code cracks it into the descendant. Thank. - Polyakov Sergey
    • if you use the word base to gain access to the members of the base class, this means that you refer to the members of this particular instance of the descendant class, which are inherited from the parent class. It is to the members of the descendant, and not to some members of some instance of the ancestor class. > And did I understand correctly that, when inheriting, a descendant takes the code from the base code, and not the base code, it cracks the descendant into something vague. Try to explain more specifically - DreamChild
    • Thanks for the answer !! With the second question, I have such a dilemma: either the descendant class takes the code from the parent, or the parent initializes its fields and gives it to the descendant during inheritance. For example, the parent has a constructor with parameters. The child calls this constructor and the parent initializes its fields. But then what ?? After the parent has initialized it passes these initialized values ​​to the descendant or does their descendant take it to itself ?? The question of something is impossible to formulate in human language. Thanks - Polyakov Sergey
    • one
      In what sense does “pick up code”? A constructor is a function; it simply starts another function at the beginning: the constructor of the base class. (Almost at the beginning.) - VladD
    • one
      > After the parent has initialized, it passes these initialized values ​​to the descendant, or does their descendant take it to itself ?? A descendant does not take anything anywhere. Because there is no separate parent object - all data that is defined in the ancestor (at the declaration level) is initially contained in the descendant. That is, if we have an ancestor declared P1 and a descendant of P2, and we create an object of type P2, this does not mean that 2 objects will be created in the compiled program, which somehow communicate with each other. - user6550