Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » Visual C# » Differentiate Hiding and Overriding

Differentiate Hiding and Overriding

As most of you know hiding and overriding are two main features based upon inheritance, which is one of the pillars of the OOP. Using these we can redefine a member of the base class in a derived class. But what are the differences. Let me clear this with some simple examples.

Author Rank :
Page Views : 24820
Downloads : 200
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
WebApplication1.zip
 
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
Nevron Chart
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

As most of you know hiding and overriding are two main features based upon inheritance, which is one of the pillars of the OOP. Using these we can redefine a member of the base class in a derived class. But what are the differences. Let me clear this with a simple example.

Start a new web application project with a default web form named "WebForm1" using visual studio. Add 2 class to this project with names "BaseClass" and "DerivedClass". The "DerivedClass" should be a subclass of "BaseClass" (i.e. should define like DerivedClass: BaseClass).

Define the following functions in the base class named "BaseClass". Notice the keyword "virtual". 

public virtual string FunctionToOverride()

{

          return "This is from base class FunctionToOverride";

}

 

public string FunctionToHide()

{

          return "This is from base class FunctionToHide";

}

 

Now define the following functions in the sub class i.e. in class "DerivedClass". Notice the keyword "new". 

 

public override string FunctionToOverride()

{

return "This is from derived class FunctionToOverride";

}

 

public new string  FunctionToHide()

{

          return "This is from derived class FunctionToHide";

}

 

Paste a label control in the webform and just write below code in the load event of form

 

private void Page_Load(object sender, System.EventArgs e)

{

          DerivedClass der = new DerivedClass();

          Label1.Text =  der.FunctionToOverride();

}

 

You will see the message form derived class function "FunctionToOverride()" as it's overrides the base class function "FunctionToOverride()". Now you just remove the "virtual" key word from the function "FunctionToOverride()" of base class i.e. define it like below

 

public string FunctionToOverride()

{

          return "This is from base class FunctionToOverride";

}

 

If you try to compile it error will generate. So we got a point that for overriding to perform keywords like "virtual", abstract etc... want to be use as modifiers in the base class.

 

So now we are going to test the hide functionality. For that just modify the code in the form load event as follows ie we are going to call the hiding function in derived class. 

 

DerivedClass der = new DerivedClass();

Label1.Text =  der.FunctionToHide();

 

You will of course get the message from the hiding function of derived class. Here hiding is performed by using the key word "new" as a modifier in the derived class function. No additional modifiers needed for base class. Now you just remove the "new" key word from derived class function "FunctionToHide()" as follows.

 

public string  FunctionToHide()

{

          return "This is from derived class FunctionToHide";

}

 

You can still run the program and can produce the same out put as just above. So another point we got as if we define 2 exactly similar functions with same prototype in a base class and derived class, "Hiding" will be the default action perform.

 

So you listening the basic differences of hiding and overriding. So here is another one just for you to try. Make the access modifiers of the base class as protected and keep that of the derived class unchanged. Then you will find that hiding has no problem after changing the base class access modifier. But program won't compile if you change the access modifier of derived class in case of overriding as it won't allow you to change the access modifier of the base class. This is an important point to care about.

 

Let me finish this article by explaining one more point in this matter. Just define the following the structure in the base class i.e. class with name "BaseClass". 

 

public struct StructToOverride

{

          String str;

          public string FunctionInStructure()

          {

                   str = "This is from base class FunctionInStructure";

                   return str;

 

          }

}

 

And add the following structure in the derived class i.e. class with name "DerivedClass".

 

public override struct StructToOverride

{

          String str;

          public string FunctionInStructure()

          {

                   str = "This is from base class FunctionInStructure";

                   return str;

 

          }

}

 

Now just try to compile the program and you will fired with an error specifying that structure is not meant for an override operation.

 

Then what about hiding? Will they support structures? Just check it by first defining the following structure in the base class.

 

public struct StructToHide

{

          String str;

          public string FunctionInStructure()

          {

                   str = "This is from base class FunctionInStructure";

                   return str;

 

          }

}

 

Then the following structure in the derived class with a keyword "new" as below

 

public new struct StructToHide

{

          String str;

          public string FunctionInStructure()

          {

                   str = "This is from derived class FunctionInStructure";

                   return str;

 

          }

}

 

Now try it from the form load event of the web form as below

 

private void Page_Load(object sender, System.EventArgs e)

{                          

WebApplication1.DerivedClass.StructToHide struct1 = new WebApplication1.DerivedClass.StructToHide();

          Label1.Text = struct1.FunctionInStructure();                         

}

 

You will get messages from the hiding structure. So another point as overriding supports methods, properties and some limited class members. But hiding supports more like that of structures.

 

My opinion is that feel free to use hiding as I think it's still not widely used. It is very helpful in case u bought a 3rd party control/dll and you want to use that control without some methods of that control/dll. So just hide them and use other functionalities. Besides you can define your own methods with the same name of those control/dll methods regardless of whether that methods are not defined as virtual/abstract. So let me finish this part and continue your part of finding more.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Jaish Mathews

An application architect. I have been in IT field since 2000 AD

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
??? by Demetrius On March 14, 2006

"So you listening the basic differences of hiding and overriding. So here is another one just for you to try. Make the access modifiers of the base class as protected and keep that of the derived class unchanged. Then you will find that hiding has no problem after changing the base class access modifier. But program won't compile if you change the access modifier of derived class in case of overriding as it won't allow you to change the access modifier of the base class. This is an important point to care about."

I do not understand what you are trying to say in these last two sentences, sorry.  Good article however but the point you are trying to make above is not clear to me and no example was provided for this "important point".

Reply | Email | Modify 
Re: ??? by Jaish On March 14, 2006

 

Hi,

Thanks for ur comment. I already said in the article that, any body found new things r always welcome. But ur idea is not cleared and seems like the things i already mentioned. Could u please confirm that the below part of my my article is urr talking about? otherwise please explain, as i am eagerly waiting ur reply.

"So you listening the basic differences of hiding and overriding. So here is another one just for you to try. Make the access modifiers of the base class as protected and keep that of the derived class unchanged. Then you will find that hiding has no problem after changing the base class access modifier. But program won't compile if you change the access modifier of derived class in case of overriding as it won't allow you to change the access modifier of the base class. This is an important point to care about."

Thanks

JaishMathews

Reply | Email | Modify 
Re: Re: ??? by sanjay On March 16, 2008

HI Jaish,

This article is very useful.A bit of confusion is in the second difference in which you are discussing about the access modifier change in case of hiding and overriding.

In case of hiding you are changing the access modifier of the base class but in case of overriding you are changing the access modifier of derived class.It sounds a bit confusing.

If you can give an example over this difference,it will be really appreciable.

Regards,

Sanjay

Reply | Email | Modify 
A question nto related to Inheritance but damn important ... by irfan On May 15, 2006

hi ,

what is the difference between "Class" and "Srtuct" with respect to 'this'  ?????

Plz answer me as quickly as possible with the help of code.

thanks a lot.

Hani.

Reply | Email | Modify 
Re: A question nto related to Inheritance but damn important ... by Mayank On February 1, 2011
http://msdn.microsoft.com/en-us/library/aa664471(v=vs.71).aspx
Reply | Email | Modify 
doubt by veera On July 18, 2006
hai sir,
 its veera from chennai, now im studying C# language, i am not able to understand the function of Delegates.
Plz can u help for me sir, give some notes about Delegates in C#.
Im waiting for ur notes sir!
thanking U
Reply | Email | Modify 
DevExpress Free UI Controls
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.