Object creation in C# inheritance hierarchy

Viewed 862

I have below two classes.

enter image description here

When execute this line,

Child myChildObj = new Child();

does this create separate two objects (Parent and Child) ? Or just a single child object which includes both the parent methods and attributes ?

enter image description here

Update: I want to know in CLR, whether it actually creates a Parent object (which is not accessible) at the runtime.

I have seen the following quote in tutorialspoint website.

The derived class inherits the base class member variables and member methods. Therefore the super class object should be created before the subclass is created. You can give instructions for superclass initialization in the member initialization list.

I have used the following code to verify this, and I got the same hashCode value for both child and parent objects.

Console.WriteLine("child object hashcode : "+this.GetHashCode());
Console.WriteLine("base object hashcode : "+base.GetHashCode());
5 Answers
Related