Constructors and Inheritance

Hi, was wondering if the following is correct:

when you create an object of a subclass (no matter if its using a default constructor or you have specified a specific constructor) the base class constructor is always called prior to the subclass constructor?

i also see its possible to call a base class constructor on a sub class but im a bit confused with the following.

"As described above, the standard behaviour for a subclass is to call the base class' default constructor on instantiation. There are two situations where this is inappropriate. Firstly, the base class may contain much more useful constructors that accept parameters. Secondly, if the base class does not include a default constructor the program will not compile unless an alternative is provided."

i thought every class was assigned a default constructor unless you actually defined a constructor inside the class?

im also a little lost as to why you would want to actually want to call a base class constructor on a subclass, since i thought the subclass constructor was always suppose to be more comprehensive than the base class one.

Thanks for your time n help

Rgds

Fraz

Comments

  • ANY time you specify a constructor with parameters in a class - the default constructor will no longer exist and you must then also write the default constructor if you want there to still be one. Now, when you are inheriting from a class - you are only "adding" to that class. The base class still needs to be constructed, so if you've written a constructor with parameters and not specified a parameterless constructor then you will need to provide values for the base class to set itself up.

    For example - pretend you have this base class:
    [code]
    public class MyBaseClass
    {
    public string SomeImportantValue { get; set; }

    public MyBaseClass(string impValue)
    {
    SomeImportantValue = impValue;
    }

    }
    [/code]

    As far as C# knows that "impValue" is imperative to the construction of that class. C# isn't smart enough to be able to know wht kind of default value should go there because for all C# knows this could require very specific values. So in order to be able to build on to this class C# is going to want a value to instantiate the base. You can still build a constructor for your inheriting class.

    [code]

    public class MyNewClass : MyBaseClass

    {

    public MyNewClass(string baseValue) : base (baseValue)
    {
    //you can construct your stuff here.
    }

    }

    [/code]

    hope that clears things up.
  • aye ok, so just to make sure i have this right, the following 2 scenarios are possible.

    1) You dont specify a constructor in your Base Class and you specify a constructor in your Sub Class. Base Class DEFAULT constructor sets everything to "0" or "null" and then Sub Class constructor changes these values (not all variables may be included in the Sub Class constructor so some of the variables may still be set as "0" or "null")

    2) You specify a constructor in both your Base Class and Sub Class which override the default ones. The variables are then set according to your Base Class Constructor and then still possibly changed by your Sub Class constructor when it is called.


    Last question, ive read you can call a base constructor instead of your Sub Class one. Im assuming this is done when your using a default constructor in your Sub Class and you have defined a constructor in your Base Class?

    when would you ever do this though?

    Thanks again DataDink + hope u have a nice wknd

    Rgds

    Fraz
  • 1, 2 - yes, this sounds correct.

    And if you have a constructor defined in your base class with parameters - then you must always define a constructor in your sub classes. Even if it's parameterless - it still has to set the values for your base class' constructor.

    And yes, you may run into a few times when the fact that you are writing a subclass means that you know what the default values should be.
  • Perfect, thanks again Datadink, much appreciated

    : D
  • Sorry I seem to have double posted n can't delete it.is it even possible to delete posts?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories