Hi,
I have two classes in Inheritance relationship and I want to know Which class constructor fires first in a parent child relationship.?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
SenthilkumarPosted Apr 26, 2012, 11:57 PM
It will call the base class constructor first in the inheritance relationship.
Let us first create the inherited class.
public class myBaseClass
{
public myBaseClass()
{
// Code for First Base class Constructor
}
public myBaseClass(int Age)
{
// Code for Second Base class Constructor
}
// Other class members goes here
}
public class myDerivedClass : myBaseClass
// Note that I am inheriting the class here.
{
public myDerivedClass()
{
// Code for the First myDerivedClass Constructor.
}
public myDerivedClass(int Age):base(Age)
{
// Code for the Second myDerivedClass Constructor.
}
// Other class members goes here
}
Now, what will be the execution sequence here:
If I create the object of the derived class as:
myDerivedClass obj = new myDerivedClass()
Then the sequence of execution will be:
public myBaseClass() method.
and then public myDerivedClass() method.
For more info about the inheritance and constructors:
http://www.codeproject.com/Articles/7011/An-Intro-to-Constructors-in-C
http://www.csharp-station.com/Tutorials/lesson08.aspx