How do you call a Base Class Constructor from Derived Class Constructor in .NET?

During inheritance, constructors are not inherited. But .NET provides you an option to explicitly call a base class constructor from the derived class constructor using the keyword “base”.



This is demonstrated below with an example from C#:

class baseClass {
int member1;
public baseClass() {
member1=1;
}
public baseClass(int data) {
member1 = data;
}
}
class derivedClass:baseClass {
int member2;
public derivedClass() : base() {
member2 = 2;
}
public derivedClass(int data1, int data2) : base(data1) {
member2 = data2;
}
public void displayMsg() {
Console.WriteLine(“Member1 = {0}, Member2 = {1}”, member1, member2);
}
}
class testClass {
public static void Main() {
derivedClass obj = new derivedClass(10, 20);
obj.displayMsg();
derivedClass obj1 = new derivedClass();
obj1.displayMsg();
}
}

Output of this code will be:

Member1 = 10, Member2 = 20
Member1 = 1, Member2 = 2

In this example, you have called the no-argument constructor and one-argument constructor of base class explicitly from the derived class constructor.

Note that calling a base class constructor from derived class constructor will execute only the matching constructor of the immediate base class. It does not trigger matching constructors in all base classes of the derived class in the hierarchy. Consider the following example:

class baseClass {
public baseClass { }
public baseClass(string msg) {
Console.WriteLine(“Passed the following message to baseClass:” + msg);
}
}
class derivedClass1:baseClass {
public derivedClass1(string msg) {
Console.WriteLine(“Calling derivedClass1 constructor…”);
}
}
class derivedClass2:derivedClass1 {
public derivedClass2(string msg):base(msg) {
Console.WriteLine(“Calling derivedClass2 constructor..”);
}
}
class testClass {
public static void Main() {
derivedClass2 obj = new derivedClass2(“Hello”);
}
}

Output of this code will be:

Calling derivedClass1 constructor…
Calling derivedClass2 constructor…

You have called the base class constructor from derivedClass2 using the keyword base. It will trigger only its immediate base class derivedClass1 and therefore you get the above shown output. If you want the next higher level of base class named baseClass to be triggered, then you have to modify the derivedClass1 constructor as shown below:
public derivedClass1(string msg):base(msg) {
Console.WriteLine(“Calling derivedClass1 constructor…”);
}

Now you will get the following output if you execute the whole program:

Passed the following message to baseClass:Hello
Calling derivedClass1 constructor…
Calling derivedClass2 constructor…

Also note that when you call a base class constructor, you have to ensure that the constructor with the specified signature already exists. If not, then you will end up in compilation error. This is demonstrated in the code shown below:

class baseClass {
public baseClass() {
Console.WriteLine(‘Calling baseClass constructor…”);
}
}
class derivedClass1:baseClass {
public derivedClass1(string msg):base(msg) {
Console.WriteLine("Calling derivedClass1 constructor…");
}
}
class testClass {
public static void Main() {
derivedClass1 obj = new derivedClass1("Hello");
}
}

In this example, derivedClass1 constructor calls the baseClass constructor with one argument. This doesn’t exist in baseClass. Hence during compilation, you will end up in the following error:
No overload for method ‘baseClass’ takes ‘1’ arguments

| How do you prevent a class from overriding in .NET? | How are classes related to objects in .NET Application | How are Delegates different from Events in .NET? | How are system exceptions different from application exceptions in .NET? | How are Value Types different from Reference Types in .NET? | How can a finalize method be suppressed in .NET? | How can you call Stored Procedure in ADO.NET? | How can you force Dispose method to be called automatically in .NET? | How do you call a Base Class Constructor from Derived Class Constructor in .NET? | How do you connect your VB.NET application to SQL Server? | How do you implement Cloning in .NET? | How do you implement Façade Design Pattern in .NET? | How do you implement MVC Pattern in ASP.NET? | How do you install .NET Assembly in GAC? | How is shadowing different from overriding in .NET? | How to prevent a particular .NET DLL from being decompiled? | Illustrate Delay Signing Process of an Assembly in .NET? | What are Reference Types in .NET? | What are the advantages of C#? | What are the advantages of VB.NET? | What are the differences between Namespace and Assembly in .NET? | What are the similar features between class and structure in .NET? | What are Value Types in .NET? | What do you mean by mixed mode authentication in .NET? | What do you mean by Satellite Assembly in .NET? | What do you mean by shadowing in .NET? | What is CTS in .NET? | What is ILDASM in .NET? | What is Managed Code in .NET? | What is Manifest in .NET? | What is MSIL in .NET Framework? | What is the importance of finalize method in .NET? | What is the need for Visitor Pattern in C#? | What is the purpose of bindingRedirect tag in web.config file of .NET? | What is the purpose of CodeDom in .NET? | What is the purpose of dispose method in .NET? | What is the purpose of Ngen.exe in .NET? | What is the purpose of Strong Name in COM Components of .NET? | What is the purpose of virtual keyword in .NET? | What Object Oriented Principles can be incorporated in .NET Application? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.