2

Hope I'm not asking something that has been already answered in here.

If my class B inherits from class A, does new B() create two instances in heap where B instance contains pointer to A instance, or there will be created only one instance of B including A members?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
cembo
  • 1,039
  • 2
  • 9
  • 18
  • https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/inheritance – TheGeneral Jun 05 '18 at 05:41
  • 7
    Doesn't it suck when you ask a question, and someone gives you a link to the documentation where you have to figure it all out for your self... However this is the most efficient way to learn things – TheGeneral Jun 05 '18 at 05:42
  • 2
    But I don't see how the article explains the question at all. The question is about *how* inheritance is actually implemented in *.NET* - as in how is the memory actually allocated and how does the derived instance access the base instance. It doesn't even have to do anything with C# even though it's tagged as such. At least that's how I understood the question. – Freggar Jun 05 '18 at 05:53
  • 1
    @Freggar See my answer, the picture is taken from this article. – Martin Verjans Jun 05 '18 at 06:22
  • 1
    @cembo actually the point was there is a ton of articles on this topic on msdn alone, and its easily searchable. asking this sort of question wastes your own time – TheGeneral Jun 05 '18 at 06:23
  • @TheGeneral ... sure, no problem, I was just curious about how is it actually implemented at lower level. I tried to find an answer but I just couldn't find anything. I just wanted to know what compiler does in case of chained inheritance and so. Still learning :) – cembo Jun 05 '18 at 06:32

4 Answers4

4

It will create one instance of B, and that B is also an A.

4

From Microsoft Inheritance article :

This is how an object is represented in memory, given the Class Publication directly inherits the class Object.

enter image description here

So a inherited class is an object that contains all information about itself but also about its base class.

Martin Verjans
  • 4,675
  • 1
  • 21
  • 48
  • so in case of chained inheritance I will practically end up creating only one instance with multiple constructors each initializing their "own" members ? – cembo Jun 05 '18 at 06:51
  • 1
    @cembo well you will create one instance. When this instance will be created, one of it's constructors will be called. Before executing this constructor, it will call it's parent class' constructor. And so on until Object's constructor is called. Each constructor has the responsibility to initialize it's class members. – Martin Verjans Jun 05 '18 at 07:12
  • of course, thanks. Hope I'm not missing some clue in here, but it still puzzles me why constructor is part of an instance in memory (as I can see in your diagram) if it's used only once for members' initialization. Besides this description for this diagram says that it illustrates relationship between two classes not structure of instance in heap ... – cembo Jun 06 '18 at 06:36
  • 1
    @cembo A constructor is nothing else than a method. Actually the methods of a Type are not copied for each instance. Each instance owns a reference pointing to where the Type's methods are located. See https://www.codeproject.com/Articles/20481/NET-Type-Internals-From-a-Microsoft-CLR-Perspecti , section Memory Layout. – Martin Verjans Jun 09 '18 at 04:31
1

it will create B instance that (because of inheritance) already include A members, if i understood your question well

evals
  • 1,750
  • 2
  • 18
  • 28
1

It creates one instance and you can access all the members of both A and B on that instance. As stated A is of type B as well. I imagine that in the low level code there probably exists a pointer to A.

Nick Gallimore
  • 1,222
  • 13
  • 31