overriding non virtual methods, why bother with override at all then

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • news.microsoft.com

    overriding non virtual methods, why bother with override at all then

    Hi,

    It is possible to override a non virtual method with the "new" keyword

    So how is this different from specifying a method as virtual then
    providing the override keyword?

    Is there any differences between these two methods of overriding?

    Thanks.


  • Ray Hsieh (Djajadinata)

    #2
    Re: overriding non virtual methods, why bother with override at allthen

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    The difference is that when you make a method virtual, it's virtual all
    the way up. When you *new* a non-virtual method, however, the base
    class's version of the method is *hidden*, so up the inheritance
    hierarchy, that method is still non-virtual. It is only virtual from the
    class where you specify new virtual, down the inheritance chain.

    news.microsoft. com wrote:

    | Hi,
    |
    | It is possible to override a non virtual method with the "new" keyword
    |
    | So how is this different from specifying a method as virtual then
    | providing the override keyword?
    |
    | Is there any differences between these two methods of overriding?
    |
    | Thanks.
    |
    |


    - --
    Ray Hsieh (Djajadinata)
    ray underscore usenet at yahoo dot com
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.3 (MingW32)
    Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

    iD8DBQE/m/slwEwccQ4rWPgRA sLyAJ9WdUuOU0vO Pq5eLc03piMrxVT PygCeOou4
    fAIjR2+mPM5rUBd +fqyYJnk=
    =fN0g
    -----END PGP SIGNATURE-----

    Comment

    • Nicole Calinoiu

      #3
      Re: overriding non virtual methods, why bother with override at all then

      There is a difference, and it lies in the behaviour of objects cast down to
      their base classes. For example, let's say you have a class A which has a
      virtual method. Class B inherits from A and overrides the virtual method.
      If you cast down an object of type B to type A and execute the overridden
      method, you will be executing the implementation of the method defined by
      class B. However, if the method is not virtual, and it is hidden by a new
      method in class B, execution of the method against an object of type B cast
      down to type A will result in execution of the class A implementation of the
      method. Run the following console application to verify this for yourself:

      class ConsoleDemo
      {
      static void Main()
      {
      Console.WriteLi ne("Working with a B object:");
      B b = new B();
      b.ShowOverride( );
      b.ShowNew();

      Console.WriteLi ne();
      Console.WriteLi ne("Working with a B object cast down to an A
      object:");
      A a = (A)b;
      a.ShowOverride( );
      a.ShowNew();

      Console.ReadLin e();
      }
      }

      class A
      {
      public virtual void ShowOverride()
      {
      Console.WriteLi ne("A.ShowOverr ide()");
      }

      public void ShowNew()
      {
      Console.WriteLi ne("A.ShowNew() ");
      }
      }

      class B : A
      {
      public override void ShowOverride()
      {
      Console.WriteLi ne("B.ShowOverr ide()");
      }

      public new void ShowNew()
      {
      Console.WriteLi ne("B.ShowNew() ");
      }
      }

      "news.microsoft .com" <anonymouse@dis cussions.micros oft.com> wrote in message
      news:%23J$HcH9m DHA.644@TK2MSFT NGP11.phx.gbl.. .[color=blue]
      > Hi,
      >
      > It is possible to override a non virtual method with the "new" keyword
      >
      > So how is this different from specifying a method as virtual then
      > providing the override keyword?
      >
      > Is there any differences between these two methods of overriding?
      >
      > Thanks.
      >
      >[/color]


      Comment

      • Jeff Louie

        #4
        Re: overriding non virtual methods, why bother with override at all then

        This should help:

        Chapter 8 "Shadow Fields, Override Virtual Methods" 

        Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


        Regards,
        Jeff[color=blue]
        >It is possible to override a non virtual method with the "new" keyword[/color]
        So how is this different from specifying a method as virtual then
        providing the override keyword?
        Is there any differences between these two methods of overriding?<



        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        Working...