locked
New Vs Override ?

    Question

  • What is the main difference between the New and Override keywords?

    Under what conditions must you use new ? and when must you use override?

     

    Friday, June 30, 2006 7:20 PM

Answers

  • http://msdn2.microsoft.com/en-us/library/435f1dw2.aspx

    The new modifier instructs the compiler to use your implementation instead of the base class implementation. Any code that is not referencing your class but the base class will use the base class implementation.

    http://msdn2.microsoft.com/en-us/library/ebca9ah3.aspx

    The override modifier may be used on virtual methods and must be used on abstract methods. This indicates for the compiler to use the last defined implementation of a method. Even if the method is called on a reference to the base class it will use the implementation overriding it.

    public class A
    {
       public virtual void One();
       public void Two();
    }

    public class B : A
    {
       public override void One();
       public new void Two();
    }

    B b = new B();
    A a = b as A;

    a.One(); // Calls implementation in B
    a.Two(); // Calls implementation in A
    b.One(); // Calls implementation in B
    b.Two(); // Calls implementation in B

    Friday, June 30, 2006 10:06 PM
  • thx a lot!! the example you gave is so crisp and easy to understand!.

     

    Friday, June 30, 2006 11:28 PM

All replies

  • http://msdn2.microsoft.com/en-us/library/435f1dw2.aspx

    The new modifier instructs the compiler to use your implementation instead of the base class implementation. Any code that is not referencing your class but the base class will use the base class implementation.

    http://msdn2.microsoft.com/en-us/library/ebca9ah3.aspx

    The override modifier may be used on virtual methods and must be used on abstract methods. This indicates for the compiler to use the last defined implementation of a method. Even if the method is called on a reference to the base class it will use the implementation overriding it.

    public class A
    {
       public virtual void One();
       public void Two();
    }

    public class B : A
    {
       public override void One();
       public new void Two();
    }

    B b = new B();
    A a = b as A;

    a.One(); // Calls implementation in B
    a.Two(); // Calls implementation in A
    b.One(); // Calls implementation in B
    b.Two(); // Calls implementation in B

    Friday, June 30, 2006 10:06 PM
  • thx a lot!! the example you gave is so crisp and easy to understand!.

     

    Friday, June 30, 2006 11:28 PM
  • Hi Andreas Johansson, I think your given solution is not correct ........................because its not working fine

    a.One(); // it will not Calls implementation in B,it will always call implementation of A..

     

    Please try this for difference of override and new keyword


    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace ConsoleApplication2
    {
        public class ChildOne
        {
            public virtual int Add()
            {
                return 1;
            }
        }
        public class ChildTwo : ChildOne
        {
            public override int Add()
            {
                return 2;
            }
        }
        public class ChildOneNew
        {
            public int Add()
            {
                return 3;
            }
        }
        public class ChildTwoNew : ChildOneNew
        {
            public new int Add()
            {
                return 4;
            }
        }
        public class MainClass
        {
            public static void Main()
            {

                ChildOne O1 = new ChildOne();
                int i1 = O1.Add();                  //OUTPUT  1
                ChildTwo O2 = new ChildTwo();
                int i2 = O2.Add();                  //OUTPUT  2
                ChildOne O3 = new ChildTwo();
                int i3 = O3.Add();                  //OUTPUT  2(call implementation of ChildTwo Class)
               

             ChildOneNew O5 = new ChildOneNew();
                int j1 = O5.Add();                  //OUTPUT  3
                ChildTwoNew O6 = new ChildTwoNew();
                int j2 = O6.Add();                  //OUTPUT  4
                ChildOneNew O7 = new ChildTwoNew();
                int j3 = O7.Add();                  //OUTPUT  3(call implementation of ChildOneNew Class)
                          
            }
        }
    }

    Thursday, May 03, 2007 9:28 AM
  • Tanvir,

    The following call works as expected:
    a.One();

    Andreas Johansson's solution is valid.
    Your code works well too.

    No difference between both the codes and both of them works as expected.

    Check once again.
    Ping us if you still need a clarification.

    -periasamy
    Wednesday, June 17, 2009 11:46 AM
  • Hello

    I was reading about difrrence between vew and overide when i saw this line 

    A a = b as A; can u help me understand it?

    Thx.

    Saturday, December 29, 2012 5:01 PM