Object Initialization – The Tricky Parts of Inheritance

Object Initialization – The Tricky Part of Inheritance

Inheritance is the basic concept of Object Oriented Programming & is often taken for granted sometimes. But there are some scenarios that you may find complicated while initializing objects in inheritance.

I will be covering such some scenarios that are the basics but still many of us may get confused when facing them.

Let’s get started by considering a common inheritance scenario:

ClassA {...}

ClassB extends ClassA {...}

This is a simple code snippet of inheritance showing the above inheritance, that we will be using to understand the object initialization in inheritance.

public class ClassA
{
    public ClassA()
    {
        Console.WriteLine("ClassA object created");
    }
}

public class ClassB : ClassA
{
    public ClassB()
    {
        Console.WriteLine("ClassB object created");
    }
}

class Program
{
    public static void Main(string[] args)
    {
        ClassA obj = new ClassA();
    }
}

This is really the most basic scenario of inheritance.

The Tricky Part of Inheritance

ClassA a = new ClassA();

ClassA a = new ClassB();

ClassB b = new ClassA();

ClassB b = new ClassB();

These are the 4 statements that come to my mind at first when seeing the most basic example of inheritance. These are amongst the most frequently asked questions in inheritance.

 

ClassA a = new ClassA();

This is a completely valid statement.

Here, we are creating an object of ClassA & assigning it to a variable of type ClassA.

A new object of ClassA will be created and will be assigned to the variable a of type ClassA.

The constructor of ClassA will be called in this scenario.

Console Output:

> ClassA object created

ClassA a = new ClassB();

This is a completely valid statement.

Here, we are creating a new object of ClassB & assigning it to the variable of ClassA. Since ClassB inherits from ClassA, we will only be able to use methods & properties of ClassA in the object a.

The constructor of ClassA will be called first, then the constructor of ClassB will be called.

Console Output:

> ClassA object created
> ClassB object created

 

ClassB b = new ClassA();

This is an invalid statement.

We can create an object of ClassA but we cannot assign it to the variable of ClassB. This will throw an invalid cast exception.

But why? The properties & methods of ClassA (the object we are initializing) does not exist in ClassB (the assigning variable type).

 

ClassB b = new ClassB();

This is a completely valid statement.

We are creating an object of ClassB & assigning it to a variable of type ClassB. This is a valid statement as the types of both LHS & RHS are same.

Since ClassB inherits ClassA, so initializing the object will first call the constructor of ClassA & then the constructor of ClassB will be called.

Console Output:

> ClassA object created
> ClassB object created

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.