• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 965
  • Last Modified:

Inheritence in C#.Net

I have a base class named as MyBaseClass and derived class as MyInheritedClass whihc inherits from MyBaseclass in C#.net

In Form1.cs I am clreating an instance of both base and derived as below.

MyInheritedClass MyInherited = new MyInheritedClass() ;

MyBaseClass MyBase ;

what am I doing here by the below syntax..
                  
MyBase = MyInherited;

I understand that using an instance of inherited class I can acccess the members of both base and dervied class.
But I want to know what can I do using the instance MyBase declared above and assigned to MyInherited (a derived class instance)..

Please let me know if I am not clear.

Thanks.
0
naveenraj
Asked:
naveenraj
  • 2
  • 2
1 Solution
 
YurichCommented:
if you downcast, from inherited to the base class, you can use function declared in your base class but you can't use function declared in your inherited class.

I normally use it when I have some custom controls, say custom drop down list, and if I want to implement some simple operations, I would cast my sender directly to ComboBox rather than to my custom control.

Regards,
Yurich
0
 
NipNFriar_TuckCommented:
When you do:

MyBase = MyInherited;

you are doing an implicit upcast... another way to write this is:

MyBase = (MyBaseClass)MyInherited;

so now you have the functionality of MyBaseClass, however in memory is everything for MyInheritedClass.  What this means is that if you then downcase MyBase you have the full functionlity of the original class.  i.e.

MyInheritedClass mic = (MyInheritedClass) MyBase;

HTH
0
 
naveenrajAuthor Commented:
Hi,

 I am not clear enough.

If we are doing as below

MyBaseClass MyBase  = new MyBaseClass();
I am creating an instance of base class MyBaseClass and using this instance I can access the members of base class. Similarly for derived class, using the command MyInheritedClass MyInherited = new MyInheritedClass();

What is the difference between the following 2 types of creating instances.

Type 1:
MyInheritedClass MyInherited = new MyInheritedClass() ;
MyBaseClass MyBase ;            
MyBase = MyInherited;

Type 2:
MyBaseClass MyBase = new MyInheritedClass() ;
MyInheritedClass MyInherited ;            
MyInherited = MyBase ;

I need a basic level of explanation.

0
 
naveenrajAuthor Commented:
I didn't get a answer for my above question yet? Please help...

Thanks
0
 
NipNFriar_TuckCommented:
The difference is that in your type 1 example you are upcasting, which is done automatically.  However, int your type 2 example you are trying to downcast, which the language specification is that you must explicitly define this cast... i.e. MyInherited = (MyInheritedClass) MyBase;.  So if I were to expand your type 2 example to what is actually happening it would look like this:

MyBaseClass MyBase = (MyBaseClass) new MyInheritedClass() ;      // Notice the cast to (MyBaseClass) that happens implicitly
MyInheritedClass MyInherited;
MyInherited = MyBase;
// At this point MyBase has all the functionality to all of MyInheritedClass... but it is only allows access to the functionality in MyBaseClass, and in  anycase this statement will cause a compile time error in C#.

- However -

MyInherited = (MyInheritedClass) MyBase; // At this point MyBase will cast successfully to MyInheritedClass and will allow access to the full funtionality of MyInheritedClass.

     
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now