Versioning

Versioning
Versioning, in the context of inheritance, is a C# mechanism that allows modification of classes (creating new versions) without accidentally changing the meaning of the code. Hiding a base class member with the methods previously described generates a warning message from the compiler. This is because of the C# versioning policy. It's designed to eliminate a class of problems associated with modifications to base classes.
Here's the scenario: A developer creates a class that inherits from a third-party library. For the purposes of this discussion, we assume that the Contact class represents the third-party library. Here's the example:
public class Contact
{
// does not include FullAddress() method}public class SiteOwner : Contact
{
public string FullAddress()
{
string fullAddress = mySite.ToString();return fullAddress;
}
}
In this example, the FullAddress() method does not exist in the base class. There is no problem yet. Later on, the creators of the third-party library update their code. Part of this update includes a new member in a base class with the exact same name as the derived class:

public class Contact
{
private string address;private string city;private string state;private string zip;public string FullAddress()
{
string fullAddress =address + '\n' +city + ',' + state + ' ' + zip;return fullAddress;
}
}
public class SiteOwner : Contact
{
public string FullAddress()
{
string fullAddress = mySite.ToString();return fullAddress;
}
}
In this code, the base class method FullAddress() contains different functionality than the derived class method. In other languages, this scenario would break the code because of implicit polymorphism. However, this does not break any code in C# because when the FullAddress() method is called on SiteOwner, it is still the SiteOwner class method that gets called.

This scenario generates a warning message. One way to eliminate the warning message is to place a new modifier in front of the derived class method name, as the following example shows:
using System;public class WebSite
{
public string SiteName;public string URL;public string Description;public WebSite()
{
}
public WebSite( string strSiteName, string strURL, string strDescription )
{
SiteName = strSiteName;
URL = strURL;
Description = strDescription;
}
public override string ToString()
{
return SiteName + ", " +URL + ", " +Description;
}
}
public class Contact
{
public string address;public string city;public string state;public string zip;public string FullAddress()
{
string fullAddress =address + '\n' +city + ',' + state + ' ' + zip;return fullAddress;
}
}
public class SiteOwner : Contact
{
int siteHits;string name;
WebSite mySite;
public SiteOwner()
{
mySite =
new WebSite();
siteHits = 0;
}
public SiteOwner(string aName, WebSite aSite)
{
mySite =
new WebSite(aSite.SiteName,aSite.URL,aSite.Description);
Name = aName;
}
new public string FullAddress()
{
string fullAddress = mySite.ToString();return fullAddress;
}
public string Name
{
get{
siteHits++;
return name;
}
set{
name =
value;
siteHits = 0;
}
}
}
public class Test
{
public static void Main()
{
WebSite mySite =
new WebSite("Le Financier","http://www.LeFinancier.com","Fancy Financial Site");
SiteOwner anOwner =
new SiteOwner("John Doe", mySite);string address;
anOwner.address = "123 Lane Lane";
anOwner.city = "Some Town";
anOwner.state = "HI";
anOwner.zip = "45678";
address = anOwner.FullAddress();
// Different ResultsConsole.WriteLine("Address: \n{0}\n", address);
}
}
Here's the output:
Address:
Le Financier,
http://www.LeFinancier.com, Fancy Financial Site

This has the effect of explicitly letting the compiler know the developer's intent. Placing the new modifier in front of the derived class member states that the developers know there is a base class method with the same name, and they definitely want to hide that member. This prevents breakage of existing code that depends on the implementation of the derived class member. With C#, the method in the derived class is called when an object of the derived class type is used. Likewise, the method in the base class is called when an object of the Base class type is called. Another problem this presents is that the base class may present some desirable new features that wouldn't be available through the derived class.
To use these new features requires one of a few different workarounds. One option would be to rename the derived class member, which would allow programs to use a base class method through a derived class member. The drawback to this option would be if there were other classes relying upon the implementation of the derived class member with the same name. This scenario will break code and, for this reason, is considered extremely bad form.
Another option is to define a new method in the derived class that called the base class method. This allows users of the derived class to have the new functionality of the base class, yet retain their existing functionality with the derived class. While this would work, there are maintainability concerns for the derived class.
Category: , 0 comments

Leave a comment