
.NET 1.1+C# Inheritance and Constructors (2)
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.
Finalizers and Inheritance
Finalizers, or destructors, are similar to constructors in inheritance scenarios, as these too are not inherited. However, as a finalizer cannot be called directly, this is not a drawback.
When an object is out of scope and being removed from memory, its finalizer is called automatically. If the object is based upon a subclass, the superclass' destructor will be called too. This happens in the reverse order to that of constructors. The subclass' finalizer is called first, followed by the base class' finalizer.
The order of execution of finalizers can be demonstrated by modifying the earlier example. Begin by adding a destructor to the "MyBaseClass" class as follows:
~MyBaseClass()
{
Console.WriteLine("MyBaseClass destructor called.");
}
Now add a similar finalizer to the "MySubclass" file:
~MySubclass()
{
Console.WriteLine("MySubclass destructor called.");
}
Finally, execute the program. Note the order in which the messages are displayed:
MyBaseClass constructor called.
MySubclass constructor called.
MySubclass destructor called.
MyBaseClass destructor called.
Calling Specific Base Constructors
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.
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.
*/
29 March 2008