3

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());
3
  • I'm not sure exactly how you mean, how do you distinguish between one and two objects? You are given a single reference to the created object, and as you know, that object can be used both as a Parent and as a Child Commented Mar 6, 2016 at 15:19
  • What information do the images add? Can't you just show the class definition? Commented Mar 6, 2016 at 15:25
  • 1
    As for your edit: what the author meant by "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" is perhaps "Call : base() from your constructor to call the constructor of the super class". It's not a very clear statement. Also, about the hash codes: "GetHashCode always returns identical hash codes for equal object references", so this.GetHashCode() and base.GetHashCode() return the same. Commented Mar 6, 2016 at 15:31

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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.

3 Comments

Could you please elaborate more on this answer. (using an diagram; cos this is what I actually wanted to know). And please prove the fact that your answer is correct according to Ofiicial docs.
Official docs... Capitalized too. I am afraid I do not have those But I did not make it up myself either, it is the basic implementation of inheritance in 3rd generation programming languages. It is not necessarily the only way to implement it but it is a simple, efficient and straightforward way. For compiled languages I do not see any practical alternatives and I am sure this is still the way it is done. The bits I speak of are data fields and method pointers. For virtual methods there is an extra indirection involved.
Look for vtable and "virtual method table", that should bring up some links to explanations of this stuff on the binary level.
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.