Based on some requirements, I want to add all constructors/methods of the base class into the derived class without writing methods/constructors in the derived class.
For that I have written code as shown below but it does not work. It shows error "ConsoleApplication1.TestClass2' does not contain a constructor that takes 1 arguments".
How can I fix that?
I cannot create any method or constructor in the base class. Is there any other way apart from inheriting a class?
My code:
namespace ConsoleApplication1
{
public class TestClass1
{
public TestClass1()
{
Console.WriteLine("This is base class constructor1");
}
public TestClass1(string str1,string str2)
{
Console.WriteLine("This is base class constructor2");
}
public TestClass1(string str1,string str2,string str3)
{
Console.WriteLine("This is base class constructor3");
}
public TestClass1(string str1,string str2,string str3,string str4)
{
Console.WriteLine("This is base class constructor4");
}
}
public class TestClass2 : TestClass1
{
}
class Program
{
static void Main(string[] args)
{
TestClass2 test = new TestClass2("test");
TestClass2 test1 = new TestClass2("test,test");
}
}
}
You can call the base class constructor from the child class by using the super() which will execute the constructor of the base class. Example: Javascript.
Use super(). __init__() to call the immediate parent class constructor. Call super(). __init__(args) within the child class to call the constructor of the immediate parent class with the arguments args .
A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.
Using a qualified-id to call a base class' function works irrespectively of what happens to that function in the derived class - it can be hidden, it can be overridden, it can be made private (by using a using-declaration), you're directly accessing the base class' function when using a qualified-id.
No, You'll have to explicitly add constructor to your derived classes. Base class's constructor is for base class only. You'll have to chain the constructor of your interested overload.
public class TestClass2 : TestClass1
{
public TestClass2 (string str1,string str2)
: base(str1,str2)//Call base constructor explicitly
{
}
}
Above sample shows that am particularly interested in constructor overload which takes two string parameters. In this case you are only allowed to use this overload only since TestClass2
doesn't have any other constructors defined.
Compiler will not provide default constructor for you when you define one; So there is only way you can create instance of TestClass2 is new TestClass2(arg1,arg2);
Try this:
public class TestClass2 : TestClass1
{
public TestClass2()
{
}
public TestClass2(string str1, string str2)
: base(str1, str2)
{
}
public TestClass2(string str1, string str2, string str3)
: base(str1, str2, str3)
{
}
public TestClass2(string str1, string str2, string str3, string str4)
: base(str1, str2, str3, str4)
{
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With