Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call base class constructor in derived class?

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");
        }
    }
}
like image 840
Microsoft Developer Avatar asked Feb 24 '14 13:02

Microsoft Developer


People also ask

How do you call a base class constructor?

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.

How do you call a base class constructor from a derived class in Python?

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 .

How do you access base class members in a derived class?

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.

Can we call base class method using derived class object?

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.


2 Answers

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);

like image 101
Sriram Sakthivel Avatar answered Oct 15 '22 01:10

Sriram Sakthivel


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)
    {
    }
}
like image 4
Only a Curious Mind Avatar answered Oct 15 '22 00:10

Only a Curious Mind