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.

Inheritance

In the previous part of the C# Object Oriented Programming tutorial, we examined the concept of inheritance and its application in C#. Using inheritance, methods, properties, events, indexers, operators and internal variables that are declared in a base class are automatically made available to its subclasses.

Constructors and Inheritance

Unlike the above class behaviour, constructors that are declared within a base class are not inherited by subclasses. As with any other class, a subclass with no constructors defined is provided with a default constructor that provides no explicit functionality. The constructors from the base class are not made available when creating new subclass object instances.

When a subclass is instantiated, the standard behaviour is for the default constructor of the base class to be called automatically. In this way, the base constructor can initialise the base class before the subclass's constructor is executed.

To demonstrate the order of execution of the constructors, examine the following sample code. The first class is the main Program class within a console application. It simply instantiates a MySubclass object. MyBaseClass is a class with a single constructor that outputs a message to the console. MySubclass inherits from MyBaseClass and also outputs to the console when constructed. Note the order in which the messages appear.

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


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


class MySubclass : MyBaseClass
{
    public MySubclass()
    {
        Console.WriteLine("MySubclass constructor called.");
    }
}

/* OUTPUT

MyBaseClass constructor called.
MySubclass constructor called.

*/

This behaviour is repeated for complex hierarchies of inherited classes. Where there are more than two levels of inheritance, the least specialised class's constructor is called first with each constructor down the tree called in turn until the specifically instantiated class' constructor is finally executed.

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's 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.
29 March 2008