Blue Theme Orange Theme Green Theme Red Theme
 
MindFusion's Components
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
World Class ASP.NET Hosting - 3 Month Free Hosting, Click Here!
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » C# Language » 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.

Technologies: .NET 1.0/1.1,Visual C# .NET
Total downloads : 128
Total page views :  15658
Rating :
 3.8/5
This article has been rated :  5 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
WebApplication1.zip
 
Become a Sponsor



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.


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Jaish Mathews
Residing in Calicut, India. Bachelors degree in mathematics. Working at Computer Science Corporation, Chennai as Project Leader. Interest areas are ASP.NET, C#, AJAX and XML
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.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
WebApplication1.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved