InterviewSolution
Saved Bookmarks
| 1. |
Can we override a constructor? |
|
Answer» No, in C#, it is necessary to define properly which constructor you are trying to call to instantiate a class and what arguments are being passed. So you cannot override a constructor in C#. Example: Below example will lead to a compilation error. // C# program to show constructor overriding is not allowed in C#using System; // Base class class Parent { Parent () { Console.WriteLine("Parent constructor"); } } // Derived class class Child : Parent { Parent() { Console.WriteLine("Child constructor"); } } // Driver Class class GFG { // Main Method static void Main() { // Create an object of Child class var obj = new Child(); } } However, you can overload a constructor in C#, for sure. |
|