Question

when I add the new keyword or I remove things behave the same way ! new keyword is supposed to reimplement the method of the base class and hide it, here's the example :

class Program
    {
        static void Main(string[] args)
        {
            BaseClass bc = new BaseClass();
            DerivedClass dc = new DerivedClass();
            BaseClass bcdc = new DerivedClass();

            Console.WriteLine("bc __________________");
            bc.Method1();
            bc.Method2();
            Console.WriteLine("dc __________________");
            dc.Method1();
            dc.Method2();
            Console.WriteLine("bcdc __________________");
            bcdc.Method1();
            bcdc.Method2();

            Console.ReadLine();
        }
    }

    class BaseClass
    {
        public void Method1()
        {
            Console.WriteLine("Base - Method1");
        }
        public void Method2()
        {
            Console.WriteLine("Base - Method2");
        }
    }

    class DerivedClass : BaseClass
    {
        public new void Method1()
        {
            Console.WriteLine("Derived - Method1");
        }
        public void Method2()
        {
            Console.WriteLine("Derived - Method2");
        }
    }

the output of bcdc will show Base - Method1 when calling bcdc.Method1(); isn't it supposed to show "Derived - Method1", if it's not the case, please explain to me why. (When using Virtual/override it works fine and I'm satisfied) but with the new keyword it works like with it or without it.

Was it helpful?

Solution

In some ways new and virtual/override are opposite keywords.

  • virtual / override: used to declare a single method with a single implementation across a range of types in an inheritance hierarchy
  • new: used to explicitly declare a new method separate from all other methods with the same signature. Not having new has the same effect it's just implicit

In this case the code has no virtual methods and hence the compiler is statically binding to the method dictated by the type of the reference on which it is invoked

OTHER TIPS

Using the new keyword will simply hide the base implementation, it will not override it. Not using the new modifier does the same thing, but implicitly.

When you define a variable as the BaseClass, it does not matter if you initialize it to a derived one. You can still only access the methods as they are defined in the base class.

BaseClass bcdc = new DerivedClass();

Console.WriteLine("bcdc __________________");
bcdc.Method1();
bcdc.Method2();

Again, using the new access modifier does not change the functionality, it only provides a clue to whoever looks at the class implementation to understand that the method is supposed to hide the base implementation.

Well, this is exactly the difference between virtual/override and new!

  • In the first case (virtual/override), the overridden method will be called, regardless of the type of variable you're invoking it from. This behavior has a name, it's called polymorphism and it's widely used in object oriented programming.
  • In the second case (new), it depends! If the variable is of a type before the new method is defined, then the old method is called when you invoke from that variable. Otherwise, the newer method is called. This is called method shadowing (the methods share the same name but they are actually different and probably unrelated - that's why you get different behavior) and it's used when a base class has introduced a new method where your derived class already had that method. To tell C# that their relationship is not virtual/override, you use new. This generally is best avoided.

Check out the C# Language Specification, page 56:

A declaration defines a name in the declaration space to which the declaration belongs. Except for overloaded members (§3.6), it is a compile-time error to have two or more declarations that introduce members with the same name in a declaration space.

When you declare your variable as a particular type, member lookups are done using that type's declaration space, with the exception of overriden members. Since you've not overriden the Method1 member, the Base class' member is discovered and used.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top