Function Overriding and Its Impact During Object Initialization


Different languages have different ways to initialize an object. C# and Java both have almost similar ways to initialize an object with few subtle differences.

Although I will not discuss about Java here but the motivation to write this article came from my experience with Java.

You would be surprised to see the output due to impact of function overriding during object initialization.

Let me walk you through with an example for better understanding.

Follow these steps :

Step 1: Create a class called BaseClass in file BaseClass.cs.

using System;
public class BaseClass
{
public int aNumber;
public virtual int aFunction()
{
Console.WriteLine("Base Class: aFunction() returning 1 called.");
return 1;
}
public BaseClass()
{
Console.WriteLine("Base Class: Constructor called.");
aNumber = aFunction();
}
}

As we can see, BaseClass is a very simple class, consisting of a field, a virtual method and a constructor.

aNumber is a field of type integer.

Virtual method aFunction prints a message and returns an integer, which is 1 in this case.

Constructor prints a message and sets aNumber field with a value returned from method aFunction.

Step 2: Add another class InheritedClass which extends class BaseClass to BaseClass.cs file as :

public class InheritedClass : BaseClass
{
public InheritedClass()
{
Console.WriteLine("Inherited Class: Constructor called.");
}
public override int aFunction()
{
Console.WriteLine("Inherited Class: aFunction returning 10 called.");
return 10;
}
public static int Main()
{
InheritedClass ic =
new InheritedClass();
Console.WriteLine("The value of aNumber is " + ic.aNumber);
return 0;
}
}

Again, InheritedClass is a very simple class, consisting of an overridden method aFunction and a constructor.

Overridden method aFunction prints a message and returns an integer. In this case it returns 10.

The constructor just prints a message.

Step 3: So far so good. Everything is so simple. But before, we proceed further, tell me, what would be the value of bNumber, 1 or 10 ?. Well I would say 10, don't believe me, just compile and run this program to verify the output yourself.

Compile the programs as :

csc BaseClass.cs

Step 4: Run the executable BaseClass.exe and see the output yourself :

Base Class: Constructor called.
Inherited Class: aFunction returning 10 called.
Inherited Class: Constructor called.
The value of aNumber is 10

As we can see, the value assigned to aNumber is 10, not 1 ie. method aFunction of InheritedClass(instead of BaseClass) was called to set aNumber field of the BaseClass. And did you notice that InheritedClass method aFunction(overridern in this class) was called even before its constructor.

If you look into C# Language Specification, you will find following statement : In a virtual method invocation, the run-time type of the instance for which the invocation takes place determines the actual method implementation to invoke.

So, if you are planning to override a method in descendant class then make sure that  same method is not used in ancestor class to assign a value to any variable of ancestor class during object initialization. Otherwise you may end up with unexpected result.

Since many VB programmers would be new to Object Oriented Programming, it is imperative for them to understand the steps involved in object initialization.

Up Next
    Ebook Download
    View all
    Learn
    View all