Constructor Chaining

Hi DataDink, just been reading over some stuff and im alittle puzzeled about 2 things.

1) if i overload the constructor in a base_class and then create an object of a sub_class which inherits the base_class. how does the compiler know which constructor to call for the base_class before calling the sub_class constructor? i know the compiler usually compares the argument list of a new object to the signatures of the various constructors to determine which one to use when creating a new object but im still not entirely sure how the above scenario is done.

2) I see its possible to call a base_class constructor using the "base" keyword when constructor chaining in a sub_class. Im guessing im missing something because this seems rather pointless if the base_class constructor has to be called before any of the sub_class constructors are.

Thanks again for your time

Rgds

Fraz

Comments

  • alright-

    You can't inherit constructors. C# is smart enough to not require you to instantiate every base class with a default constructor because there are no parameters required so C# can handle it. But once you require any kind of parameter for instantiation you have to pass something to the base class.

    Pretend you have 2 classes: "MyBase" and "MySub". MySub inherits from MyBase.

    When creating "MyBase" you only give it one constructor:
    [code]
    public MyBase(string a, string b)
    [/code]

    Since "MySub" does not inherit the constructor and there is no default constructor there is no way to instantiate this class.
    You CAN'T do this:
    [code]
    var MyVariable = new MySub("one", "two");
    [/code]

    Because of this VisualStudio will not even let you compile "MySub".

    You MUST make a constructor for "MySub" that will pass values to "MyBase" when being instantiated:
    [code]
    public MySub(string one, string two) : base (one, two)
    [/code]

    You can even create a "default" constructor for "MySub" but it would have to pass some kind of default value to the MyBase:
    [code]
    public MySub() : base("Default Hard-Coded Value 1", "Value 2")
    [code]

    This would allow you to now do something like this:
    [code]
    var MyVariable = new MySub();
    [/code]
    because MySub is passing hard-coded default values to the base in order to initialize it.

    As far as C# knowing WHICH constructor to use on a base, this is no different than how it knows which constructor to use on a sub class.

    Whatever constructor you call on a sub class will be relaying parameters to the base class - and based on the signature of the parameters being passed - C# will know which constructor to call. The omission of any relay constructor simply tells C# to use the default constructor for the base class (if possible) - if no default constructor is available and you are not relaying any parameters, then you will get a compile error and will not be able to build your program.
  • Hi, sorry for the late reply but i havent been able to access this website untill now.

    had a read of your last post n there is one thing i still dont understand.

    "You MUST make a constructor for "MySub" that will pass values to "MyBase" when being instantiated:"

    if i remember correctly, when i create an object of MySub, an object of MyBase is also created before hand. If this is the case, how does MySub pass anything to it?

    or is it that the compiler see's im trying to create an instance of MySub and then sees ive called MyBase n assigned arguments?

    hope this clarifies where im getting confused

    Cheers again

    Fraz
  • yes - if you have not specified a default constructor for MyBase - then you have to give it values.

    IF you have a default constructor then MyBase can be constructed without parameters and will be automatically instantiated when instantiating MySub.

    for example:

    [code]
    public class MyBase
    {
    public MyBase(string parm)
    {
    }
    }
    [/code]

    C# will no longer allow you to construct MyBase using a default constructor. You can't do the following:
    [code]
    var myItem = new MyBase();
    [/code]

    same goes for inheritance. You MUST relay an argument from the MySub constructor. - but only if MyBase does not have a default constructor:
    [code]
    public class MyBase
    {
    public MyBase()
    {
    }

    public MyBase(string parm)
    {
    }
    }
    [/code]

    now you can once again construct "MyBase" without parameters


  • i think i have it now.... but i dont understand why c#s compiler cant just use the constructor in the base rather than me having to pass values to it even if i have defined a constructor in both the base and the sub classes.

    ie i cant just add a prefix to a constructor in the base class, if i have more than one, so it knows exactly which one to call before creating an object of the subclass which inherits from it.

    a master base class constructor, if that makes sense?

    Thanks again

    Rgds

    Fraz
  • I think the reasoning behind it is that even though you are adding to an existing class - you technically have a new class on your hands.

    Like it wouldn't make sense to say:
    [code]
    SchoolBus _myVehicle = new Automobile();
    [/code]

    even though it would make sense to say:
    [code]
    Automobile _myVehicle = new SchoolBus();
    [/code]
  • ... lol, fair point!

    Thanks again :)
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