Saturday, July 14, 2012

Base Class, Derived Class and Headache

Good morning. 2nd post after a long time.

I’m trying to be regular but… anyway from now onwards…..

In the early days of my career I used to confuse when assigning object of one type to variable of another type. For example consider these 3 classes.

1. Grand Father 2. Father 3. Finally Me

I think by seeing the class names you must have visualized that this fellow talking about inheritance. Of course for this post I’ve taken these 3 inherited classes but I’m not going to talk about inheritance. I know you already know that theoretical stuff.



In this example, we have grandparent class, father class inherited from grandparent class and FinallyMe class inherited from the father class.

My confusion was like this.Look at the below code block.



No issues. We have defined a varable of type FinallyMe and assigned FinallyMe object to it.

Now, look at the below one



1,2 butterflies flying in the stomach.

Ofcourse, for few of you it may not look that complex. Derived class object can be referred using base class variable. What is so great about it?

If you are feeling like that, this post is not for you. If you are little discomfort or frequently forgetting which type can be referred by which type (derived with base or base with derived) then read on.

One way I remember this stuff is,

Left side variable should always be on top or equal to the right side object. Look at the below given images.



Valid case. Object variable and real object both are of same of type. Both are at the same level.



Valid case. Object variable on top level compared to the real object.



Not a valid case. Object variable on bottom level compared to the real object.

Clear on this ?

Now one more thing should be cleared. In case of second scenario where top level object variable referring to it’s derived object, what are the methods can be called ? Let me explain this scenario in detail.

Take this example again



Now confusion may come. Variable is defined of type GrandParent. It is having a method called ”GrandParentProperty”. We know we can call this method using this variable but it is assigned to an object called “FinallyMe” which is having two more methods “FatherProperty” and “MyProperty”.

Can you call these two methods also like below ?

myGrandParentObject.FatherProperty()
myGrandParentObject.MyProperty()

You cannot. Your program will not get compiled at all. Your compiler may show error like “FatherProperty” is not a member of myGrandParentObject.

Your compiler argument is this.

Variable myGrandParentObject is of type GrandParent. It is having only one method called “GrandParentProperty” How can I call “FatherProperty” or "MyProperty" ?

Your argument may be this.

I have assigned Variable myGrandParentObject to object “FinallyMe”. This object is having methods called “FatherProperty” and “MyProperty”. What is your problem in calling them then?

After some silence, your compiler may say this.

Ok. You are correct and I’m also correct. Let us come to a compromise. You just clearly tell me the original object name which actually holding the required method. Please use the word CType like below so that I can understand what I have to do.

CType(myGrandParentObject, FinallyMe).FatherProperty()
CType(myGrandParentObject, FinallyMe).MyProperty()

Now your compiler is happy and you are happy. Your program runs.

Fine. Leave all this non sense talk. I hope message is clear to you. If you want to call methods of your object from a parent variable type, just use CType. It will do the trick.

Any more questions on this concept ? Please post them in comments section.

I’m signing off here. Let us meet in another post.

Note : If any image is not clear, just click on it.

No comments:

Post a Comment