Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a base class's object is created when we create a new object of its inherited class?

Tags:

c#

.net

In C#, when we create a inherited class object is it creating a base class object also?Got confused because its calling the base class constructor from children's class constructor.

Will calling a base class constructor from child class constructor, create a base class object?

like image 428
vrnithinkumar Avatar asked Dec 09 '15 05:12

vrnithinkumar


2 Answers

Only one object is created but it has two "layers" - the base class properties and behaviours and the inherited class properties and behaviours. So in one sense the answer is "Yes, a base class object is created" (that object has the same properties and behaviours as any other base class object) but it's the same object as the inherited class so it's also true to say "No, a base object is not ALSO created.". The key difference is the "also".

The fact that one object can seem to be two different things (or more) is at the heart of object orientation. It's what makes it both powerful and complicated.

like image 165
christutty Avatar answered Oct 20 '22 06:10

christutty


No it will not create an objects of the base class, the inherited class's objects can have accessibility to the base class properties(according to the protection level). so that the particular members(available to the inherited class) are only initialized, no object of base class is created.

like image 40
sujith karivelil Avatar answered Oct 20 '22 07:10

sujith karivelil