This article is about storing object reference(address) in possibly different types of variables.
I have found in my experience that many developers are confused about object type and variable types in which it’s object can be stored. Knowing it deeply will help you to better and efficiently program your logic.
Before starting I would like to tell a general truth related to practical life:
It’s a Child’s right to live in there parent’s home, it’s by law. When a child takes birth it lives with their parents for many years. But a parent will live in home of their adult child only if their child allows so.
This rule of nature is same in Object Oriented Worldand.NET is not an exception.
I am assuming that reader of this article is familiar with Parent Object and Child Object in OOPs.
So, the core theme of this article is Child’s Object reference can be stored freely in Parent’s variable (means you can think like a Child can freely live in there Parent’s home) but the reverse is not the default behavior. A Parent class object reference can only be stored in Child’s variable only if it is done so explicitly (think like Parents can live in their Child’s home only if they are allowed so) .
In short we can say :
Up-Casting – When Child class object is referred by a variable of Parent type.
Down-Casting – When a Parent class object is referred by a variable of Child type.
In majority of situations you will face Up-Casting. Down-casting is rarely used. At least I have rarely used Down-Casting.
Now one question would have raised in your mind. What is the benefit of these programming constructs!
Believe me, Up-Casting is very important to understand and it will never leave your hand till you are in Programming world.
Many times we get objects of certain type from some other helper methods or any type of method and we are not quite sure about the actual type of that object. This happens many times while programming. If we are getting object and we are not 100% sure about the actual type then it becomes difficult for you determine the variable type in which you want to store the object reference. So, here comes the Up-Casting as a rescue.
You just have to know their Parent type and you can store object reference in variable of Parent type. But be sure that all objects that you will get are at least derive from that Parent type.
This way using a single variable of Parent type you can virtually store object of any object which derive from that Parent class and Call the objects method.
It’s reverse is not the default behavior because it is dangerous to keep parent’s object reference in Child’s type variable. Just imagine Child has some new features added, and you added object reference of Parent type explicitly, then possibly you may call that method that was declared in Child class. Now the dangerous situation is that, you have a object of Parent type and stored in variable of Child type so Child’s explicate methods will be visible in that variable even though that is pointing to Parent type object. So, this of call has runtime error. You will not get any compile time error. That’s way I called Down-Casting a dangerous situation.
I think you would now have a better idea about Up-Casting and Down-Casting and the benefit of Up-Casting.