Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. overriding

overriding

Scheduled Pinned Locked Moved C#
question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    angels777
    wrote on last edited by
    #1

    class A { public virtual void F() {} } class B: A { new private void F() {} // Hides A.F within B } class C: B { public override void F() {} // Ok, overrides A.F } may i knwo whta is Hides A.F within B and overrides A.F ?? thanks

    A 1 Reply Last reply
    0
    • A angels777

      class A { public virtual void F() {} } class B: A { new private void F() {} // Hides A.F within B } class C: B { public override void F() {} // Ok, overrides A.F } may i knwo whta is Hides A.F within B and overrides A.F ?? thanks

      A Offline
      A Offline
      Anthony Mushrow
      wrote on last edited by
      #2

      OK

      class A
      {
      public virtual void Message()
      {
      Console.WriteLine("Original Message");
      }
      }

      class B : A
      {
      public new void Message()
      {
      Console.WriteLine("Class B's Message");
      }
      }

      class C : A
      {
      public override void Message()
      {
      Console.WriteLine("Class C's Message");
      }
      }

      Then, if you create an instance of each class and call Message() you will get each classes message. But, if you cast them as class A, the results are different:

      A myA = new A();
      B myB = new B();
      C myC = new C();

      myA.Message(); //Outputs "Original Message"
      myB.Message(); //Outputs "Class B's Message"
      myC.Message(); //Outputs "Class C's Messgae"

      //Notice that these are of type A
      A myAB = new B();
      A myAC = new C();

      myAB.Message(); //Outputs "Original Message"
      myAC.Message(); //Outputs "Class C's Message"

      When you use NEW it just hides the old method, so if you cast it as the base class it won't be able to see the new method anymore and the original will be called. If you use override, then its like re-writing the original method, so even though it is cast back as it's base class the derived classes method will still be called.

      My current favourite word is: I'm starting to run out of fav. words!

      -SK Genius

      Game Programming articles start -here[^]-

      modified on Saturday, May 17, 2008 1:21 PM

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups