What is Constructor Chaining in Inheritance concept?
What is Constructor Chaining in Inheritance concept?
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.
Abhishek KumarPosted Sep 9, 2015, 3:20 AM
Pankaj Kumar ChoudharyPosted May 23, 2015, 10:34 AM
Constructor chaining occurs through the use of inheritance A subclass constructor method's first task is to call its superclass' constructor method. This ensures that the creation of the subclass object starts with the initialization of the classes above it in the inheritance chain.
There could be any number of classes in an inheritance chain. Every constructor method will call up the chain until the class at the top has been reached and initialized.
Then each subsequent class below is initialized as the chain winds back down to the original subclass. This process is called constructor chaining.
Examples:
{
string Stm;
public Class1()
{
}
public Class1(string st)
{
Stm = st;
Console.WriteLine("Class1 Constructor");
}
}
public class Class2 : Class1
{
string Stm;
public Class2(string st):base(st)
{
Stm = st;
Console.WriteLine("Class2 Constructor");
}
}
public class Class3 : Class2
{
string Stm;
public Class3(string st):base(st)
{
Stm = st;
Console.WriteLine("Class3 Constructor");
}
}
class Demo
{
static unsafe void Main(string[] args)
{
Class3 Cs = new Class3("Pankaj");
Console.ReadLine();
}
}
}