BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Inheritance and Constructors

The nineteenth part of the C# Object-Oriented Programming tutorial continues the discussion of inheritance. Constructor and destructor functionality is not inherited by subclasses but these can still use the constructors defined in their base class.

Calling Specific Base Constructors

As described above, the standard behaviour for a subclass is to call the base class's 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.

One base constructor will always be called when a new object is created. However, the choice of base constructor to execute can be changed using the base keyword. The base keyword is used in a similar manner as the "this" keyword in constructor overloading. The constructor declaration is declared as usual, then a colon character (:) is appended. After the colon the base keyword and the parameter list of the base constructor to call is provided. Each parameter specified in the call to the base class must match one of those in the new constructor or be a literal value.

public Constructor(parameters-1) : base(parameters-2)
{
}

To demonstrate the execution of base constructors, add a new constructor to "MyBaseClass". This constructor will accept a single integer parameter, which will be outputted to the console.

public MyBaseClass(int aNumber)
{
    string output;
    output = string.Format("MyBaseClass created with value {0}.", aNumber);
    Console.WriteLine(output);
}

We can now add a constructor to "MySubclass" that calls the new base class constructor when an object is instantiated:

public MySubclass(int startValue) : base(startValue)
{
    string output;
    output = string.Format("MySubclass created with value {0}.", startValue);
    Console.WriteLine(output);
}

To test the example, the Main method in the Program class must be updated. In the new version, a "MySubclass" object is created using the new constructor.

static void Main()
{
    MySubclass test = new MySubclass(10);
}

/* OUTPUT

MyBaseClass created with value 10.
MySubclass created with value 10.
MySubclass destructor called.
MyBaseClass destructor called.

*/

Using Protected Constructors

In the previous article in this series I introduced the protected scope and associated access modifier. Using this scope, a constructor can be declared as protected in a base class. This constructor will not be available for use externally except within subclasses.

To show the use of protected constructors, modify the parameter-less constructor of MyBaseClass to make it protected:

protected MyBaseClass()
{
    Console.WriteLine("MyBaseClass constructor called.");
}

If you modify the Main method as follows, you will see that the default constructor is no longer available for general use and the code will not compile.

static void Main()
{
    MyBaseClass test = new MyBaseClass();
}

The protected constructor is still available to the subclass. We can demonstrate this by modifying the Main method again. Try executing the following code.

static void Main()
{
    MySubclass test = new MySubclass();
}

/* OUTPUT

MyBaseClass constructor called.
MySubclass constructor called.
MySubclass destructor called.
MyBaseClass destructor called.

*/
29 March 2008