3

I have a parent class with 2 constructor and the derived class trying to call the constructor of parent in 2 different methods

public class Parent
{
    public Parent()
    {
        //some stuffs
    }
    public Parent(string st)
    {
        //some stuffs
    }
}

Now I have a derived class with two methods. I Have to use Parent-constructor in one method and the Parent(string st) in other method. But here It is always calling the Parent-constructor. Below is the derived class

public class Derived : Parent
{
    public void GetData()
    {
        //Here I initialize the constructor like this
        Parent p = new Parent();
    }

    public void GetData1()
    {
        string s = "1";
        Parent p = new Parent(s);
    }
}

Please let me how to make this happen. Thanks in advance.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Ram
  • 59
  • 1
  • 5
  • 1
    You're not inheriting Parent, you're creating an instance. See how to call the parent constructor here: http://stackoverflow.com/questions/12051/calling-the-base-constructor-in-c-sharp – steenbergh Nov 13 '15 at 13:00
  • 1
    Why do you create a new instance it all? You have already one, yourself. If yu made `GetData` static it would be a factory method that returns an instance. But currently it's pointless. – Tim Schmelter Nov 13 '15 at 13:01
  • @steenbergh Tim is right, your code as is, is pointless to derive from `Parent`, but you code does call constructor overload that takes a string. Why do you think it doesn't? Put a `Console.WriteLine` in that constructor, you'll see it works. – Crowcoder Nov 13 '15 at 13:07

4 Answers4

5

Just have two constructors in your Derived class that use the appropriate constructor in the base.

public class Derived : Parent
{
   public Derived() : base()
   {
   }

   public Derived(string s) : base(s)
   {
   }
}

The :base() nomenclature will invoke the parent constructor.

Steve
  • 2,950
  • 3
  • 21
  • 32
2

Use the parent constructor call "base"

public class Parent {
   public Parent() {}
   public Parent(string s) {}

}

public class Child : Parents {
   public Child():base() // Call Parent empty constructor

   public Child(string s): base(s) // Call Parent Constructor with parameter
}

Your error comes because you're instanciate a new Object Parents in your Child. With Base(), you call the parent constructor without instanciate him.

Armiseo
  • 36
  • 6
1

Calling new Derived() will call the Parent() ctor because it inherits from Parent and has an implicit Derived() : base().

After you have constructed Derived() calling the method (not constructor) GetData1() will call the Parent(string st) constructor

These are completely separate routes of constructing parent and it seem you are confusing them

James
  • 9,774
  • 5
  • 34
  • 58
1

The Parent() constructor is always called because you Derived derives from Parent but doesnt include a constructor, hence a default constructor is created, which calls the parameterless parent constructor.

The call to Parent p = new Parent(s); will call the constructor of Parent that takes a parameter.

If you want to call the parent constructor when creating a Derived object, you have to chain the constructors using base().

public class Derived : Parent
{
   public void Derived()
    : base()
   {
       //code
   }

   public void Derived(string s)
        :base(s)
   {
       //code
   }
}
Domysee
  • 12,718
  • 10
  • 53
  • 84