Wednesday, March 24, 2010

What is Versioning?

Using the new modifier to hide the inherited members is similar to overriding methods using the override modifier. Both are used to design a new version of a program while maintaining backward compatibility with previous versions of the program. For example, assume that we are inheriting a class product by Acme Company called AcmeClass:
public class AcmeClass //AcmeClass:
{
    //…
}
public class MyClass: AcmeClass // your class
{
    //…
}
Suppose that we needed to add a method of our own named MyMethod, like this:
public class MyClass: AcmeClass
{
    public virtual MyMethod()
    {
        //The new method in our program.
    }
    //…
}
Our program was working just fine until Acme produced a new version of its program that includes a method called MyMethod, which does the same work:
public class AcmeClass
{
    public virtual MyMethod()
    {
        //The new method in Acme’s program v2.0.
    }
}
Now we have a problem. The solution is to just use the new modifier to declare MyMethod in our program. This will emphasize our intention to hide the method MyMethod in the second version of AcmeClass. For example:
public class MyClass: AcmeClass
{
    public new virtual MyMethod()
    {
        //….
    }
}
Problem solved.
We can, as an alternative, use the override modifier if we plan to override MyMethod in the base class.
In the following example the class MyDerivedClass inherits the class MyBaseClass. Each class contains a member class called MyClass. In order to hide MyClass in the base class, the new modifier is used in the class declaration. This way, it becomes possible to access members in both classes and use their members.
//Example 8-5.cs
//Hiding members using the new modifier
using System;
public class MyBaseClass
{
    public class MyClass
    {
        public int myInt = 123;
        public virtual string MyMethod()
        {
            return “Hello from the base class!”;
        }
    }
}
public class MyDerivedClass : MyBaseClass
{
    //The following nested class hides the base class member:
    new public class MyClass //notice the new modifier
    {
        public int myInt = 321;
        public virtual string MyMethod()
        {
            return “Hello from the derived class!”;
        }
    }
    static void Main()
    {
        //Create an object from the “new” MyClass:
        MyClass myObj1 = new MyClass();
        //Create an object from the hidden MyClass:
        MyBaseClass.MyClass myObj2 = new MyBaseClass.MyClass();
        Console.WriteLine(“Value from the ‘new’ MyClass: {0}”, myObj1.myInt);
        Console.WriteLine(“Value from the ‘hidden’ MyClass: {0}”, myObj2.myInt);
        Console.WriteLine(“Value form the ‘new’ MyClass: {0}”, myObj1.MyMethod());
        Console.WriteLine(“Message from the ‘hidden’ MyClass: {0}”, myObj2.MyMethod());
    }
}
In the preceding example we created an object of each of the classes named MyClass. Although each class contains a field and a method with the same name as those in the other class, the new modifier was used only once in declaring the class.

No comments:

Post a Comment