Inheritance - Constructors

When you instantiate an object based on a derived class it is important to know what happens when you call the constructor.

Create a new project and add the classes below.

class ClassA

{

protected string name;


public string Name { get { return name; } }


public ClassA()

{

Console.WriteLine("This is the default constructor for Class A");

this.name = "Instance of Class A";

}

}


class ClassB : ClassA

{

public ClassB()

{

Console.WriteLine("This is the default constructor for Class B");

this.name = "Instance of Class B";

}

}


class ClassC : ClassB

{

public ClassC()

{

Console.WriteLine("This is the default constructor for Class C");

this.name = "Instance of Class C";

}

}


class MainClass

{

public static void Main(string[] args)

{

ClassC demoObject = new ClassC();


Console.WriteLine(demoObject.Name);

Console.ReadKey(true);

}

}

When the code in main is executed, it will instantiate an object of ClassC. This will call the constructor of ClassC, but also the constructors of the Classes that ClassC is derived from.

The constructor for each derived class makes an implicit call to the default constructor method of the base class before running any code within its own method.

This can be done explicitly using the keyword base.

public ClassC() : base()

Stating it explicitly can be particularly useful when there are overload constructors and you would like to specify which constructor you would like to call.

Try the code below.
Each class has a default constructor and an overload constructor that allows the name of the object to be assigned via its parameter. However it is only in ClassA that the value is actually assigned.

class ClassA

{

protected string name;


public string Name { get { return name; } }


public ClassA()

{

Console.WriteLine("This is the default constructor for Class A");

this.name = "Class A Obj";

}


public ClassA(string name)

{

Console.WriteLine("This is the overload constructor for Class A");

this.name = name;

}

}


class ClassB : ClassA

{

public ClassB() : base("Class B Obj")

{

Console.WriteLine("This is the default constructor for Class B");

}


public ClassB(string name) : base(name)

{

Console.WriteLine("This is the overload constructor for Class B");

}

}


class ClassC : ClassB

{

public ClassC() : base("Class C Obj")

{

Console.WriteLine("This is the default constructor for Class C");

}


public ClassC(string name) : base(name)

{

Console.WriteLine("This is the overload constructor for Class C");

}

}


class MainClass

{

public static void Main(string[] args)

{

ClassC demoObject = new ClassC("Explicitly named Object of ClassC");


Console.WriteLine(demoObject.Name);

Console.ReadKey(true);

}

}

To see the effect of this try changing the line where the demoObject is instantiated in the MainClass to:

ClassC demoObject = new ClassC();

Note which constructors are called and in what order.

Experiment by instantiating objects of the different classes.