Jude Niroshan
Jude Niroshan

Reputation: 4460

Object creation in C# inheritance hierarchy

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());

Upvotes: 3

Views: 927

Answers (5)

Martin Maat
Martin Maat

Reputation: 744

The answers given are all as abstract as what you learned from the books and I feel you want to know what it looks like under the covers. You can think of the child instance as a piece of memory that is exactly what you would get if you would instantiate a parent object, only with some extra bits and pieces appended to it (the child bits). The reference you get is a pointer to the beginning, that is the base class instance. This is why no one will ever see the difference between a parent instance and a child instance. Because the child instance is a parent instance (with the child bits appended to it). This goes on as you instantiate GrandChild objects, those would also be the same with yet some extra bits appended to it. Now you understand why and when casting works and when it would cause trouble.

Upvotes: 1

semiColon
semiColon

Reputation: 201

There is only one referenceable object created which is myChildObj. The parent class is used as a template but is not ultimately instantiated. This object will have all member variables and methods of the Parent class as well as the variables and methods in its own class.

It is also possible to create the object using polymorphism like this:

Parent obj = new Child();

Upvotes: 0

poke
poke

Reputation: 388393

Child is a subclass of Parent, so it inherits all its properties. But when you create an object of Child, you only get an object of Child. There is no “second part” that takes care of the Parent-related things. There only exists a single object in the memory. And that single object has the type Child and as such is also compatible to the Parent type.

Upvotes: 2

TcKs
TcKs

Reputation: 26642

It creates just one instance of class "Child". The instances of "Child" inherits (includes) all the members from "Parent" class.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039498

This will create a single instance of the Child type which includes methods from both types. You could call those methods on the created instance.

Upvotes: 3

Related Questions