10

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?

2
  • 3
    In order to inherit/initialize the member from the base class it does call the base constructor first and then child constructor. Eventually your child class instance is said to be an instance of base class also (i.e. is a relationship). Commented Dec 9, 2015 at 5:40
  • 2
    No, there will be only one object created. Think of the constructor as simply a 'setup method' which runs its logic on the current object (an instance of the child class). Commented Dec 9, 2015 at 5:47

4 Answers 4

3

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.

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

Comments

1

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.

Comments

0

No, object of base class is not created by doing so. and When an object of the child class is created then the constructor of the base class is called automatically even before calling child class constructor, so i don't think we need to call base class constructor from child class.

So generally, base class constructor is used to perform some task in the background before the actual task is started by the child class.

Comments

0

No, It will not create object of base class. But, when a derived class object is created, the constructors from the base and derived classes will run. The order in which they run is important, too -- the base class constructor runs first, then the derived.

class Program { static void Main(string[] args) { var x = new B(); } } public class A { public A() { Console.WriteLine(1); } } public class B : A { public B() { Console.WriteLine(2); } } 

If we are considering the above case, we can expect an output like below

1 2 

one exception there is, If we have static constructor in place then it will get executed first and that is in the reverse order, that means first the derived class static constructor will get executed and then base class static constructor.

class Program { static void Main(string[] args) { var x = new B(); } } public class A { public A() { Console.WriteLine(1); } static A() { Console.WriteLine(2); } } public class B : A { public B() { Console.WriteLine(3); } static B() { Console.WriteLine(4); } } 

If we are considering the above example, We can expect an output like below.

4 // derived class static constructor called 2 // base class static constructor called 1 // base class constructor called 3 //derived class constructor called 

Back to your question, the reason why the base class constructor is created during derived class object creation is :

when we inherit one class from another, the data members and member functions of base class comes automatically in derived class based on the access specifier, but the definition of these members exists in base class only. So when we create an object of derived class, all of the members of derived class must be initialized but the inherited members in derived class can only be initialized by the base class’s constructor as the definition of these members exists in base class only. This is why the constructor of base class is called first to initialize all the inherited members.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.