SIGN UP MEMBER LOGIN:    
ARTICLE

Overriding Non-Virtual Implemented Interface Methods

Posted by Graham Chow Articles | C# Language October 02, 2003
In the .NET framework many classes implement interfaces non-virtually. Presumably this is because it does not make any sense for those methods to be overridden.
Reader Level:

In the .Net framework there exist many classes that implement interfaces non-virtually. Presumably this is because it does not make any sense for those methods to be overridden. However, sometimes it is helpful to override them for debugging purposes. This article shows you how to override the non-overrideable.

There is no magic to this; it is just another way of using the "new" keyword.

Step #1

Create a derived class that inherits from the class you wish to inherit.

Step #2

Also inherit the interface that contains the methods you wish to override.

Step #3

Using the "new" keyword, implement the methods you wish to override.

An example of overriding Renewal of ClientSponsor is this is shown below:

using System;
using System.Runtime.Remoting.Lifetime;
namespace MyConsoleApplication
{
public class MyClientSponsor : ClientSponsor, ISponsor
{
/// <summary>
/// Even though ClientSponsor.Renew is not virtual, you can
/// override it like this.
/// Notice that ISponsor is implemented by this class.
/// </summary>
/// <param name="lease"></param>
/// <returns></returns>
new public TimeSpan Renewal(ILease lease)
{
System.Console.WriteLine("Overridden!");
return base.Renewal(lease);

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ISponsor s = (ISponsor)
new MyClientSponsor();
s.Renewal(
null);
}
}
}

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • 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.
    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.
Team Foundation Server Hosting
Become a Sponsor