Parameterized abstract class constructor

18,789

Solution 1

Yes, you have to supply an argument to the base class constructor.

Of course, the derived class may have a parameterless constructor - it can call the base class constructor any way it wants. For example:

public class Foo : ABC
{
    // Always pass 123 to the base class constructor
    public Foo() : base(123)
    {
    }
}

So you don't necessarily need to pass any information to the derived class constructor, but the derived class constructor must pass information to the base class constructor, if that only exposes a parameterized constructor.

(Note that in real code Foo would also have to override computeA(), but that's irrelevant to the constructor part of the question, so I left it out of the sample.)

Solution 2

You can create a default constructor in a derived class that does not need parameters and the derived class will supply default values, but you cannot remove the requirement entirely. It is a manadatory condition of the base class to have some sort of value.

public MyDerivedClass : ABC
{
  public MyDerivedClass()
    : base(123) // hard wired default value for the base class
  {
    // Other things the constructor needs to do.
  }

  public override void computeA()
  {
    // Concrete definition for this method. 
  }
}

Solution 3

Add a default constructor to the base class and call the other constructor providing an initial values for its parameters:

public abstract class ABC
{
    int _a;  
    public ABC(int a)  
    {    
         _a = a;   
    }

    public ABC() : this(0) {}    
}
Share:
18,789
logeeks
Author by

logeeks

Updated on June 04, 2022

Comments

  • logeeks
    logeeks almost 2 years

    I have a class like below

    public abstract class ABC
    {
        int _a;
        public ABC(int a)
        {
            _a = a;
        }
        public abstract void computeA();
    };
    

    Is it mandatory for the derived class to supply the parameters for the base/abstract class constructor? Is there any way to initialize the derived class without supplying the parameters?

    Thanks in advance.

  • Colin Mackay
    Colin Mackay about 13 years
    Damn... Beat me too it... And 123 does seem to be a popular default value. :-)
  • Marijn
    Marijn about 13 years
    +1 for including the override - which someone else omitted ;-)
  • LukeH
    LukeH about 13 years
    Shouldn't that be public ABC() : this(0) {} instead?
  • LukeH
    LukeH about 13 years
    Yes, it's incorrect. If you want to chain constructors then you need to use the public ABC() : this(0) {} syntax; you can't chain a call to a constructor from inside the body of another constructor.
  • Jon Skeet
    Jon Skeet about 13 years
    @Marijn: I omitted it on the grounds of it not being relevant to the constructor call, that's all :)
  • Marijn
    Marijn about 13 years
    I figured ... for a short moment I considered placing a nitpicking comment below the post, but was too intimidated by the 300k+ reputation of the author.
  • Marijn
    Marijn about 13 years
    @Jon Skeet: But seriously, given the question, I find that this is a better answer. "it compiles" and doesn't give raise to another question.
  • Jon Skeet
    Jon Skeet about 13 years
    @Marijn: Will edit my answer to add it - not the code, just an explanation.
  • Marijn
    Marijn about 13 years
    @Jon Skeet: I would never ask you to change your (pseudo-)code :-)
  • Jon Skeet
    Jon Skeet about 13 years
    @Marijn: I hope you would, if it were wrong - which happens more often than people think :)