Created
July 25, 2012 08:36
-
-
Save pmsanford/3175107 to your computer and use it in GitHub Desktop.
Demonstrate the difference between method hiding and overriding.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace MethodHiding | |
{ | |
class Program | |
{ | |
class A | |
{ | |
public void Foo() | |
{ | |
System.Console.WriteLine("A::Foo"); | |
} | |
public virtual void Bar() | |
{ | |
System.Console.WriteLine("A::Bar"); | |
} | |
} | |
class B : A | |
{ | |
public new void Foo() | |
{ | |
System.Console.WriteLine("B::Foo"); | |
} | |
public override void Bar() | |
{ | |
System.Console.WriteLine("B::Bar"); | |
} | |
} | |
static void Main(string[] args) | |
{ | |
A a = new A(); | |
a.Foo(); | |
a.Bar(); | |
B b = new B(); | |
b.Foo(); | |
b.Bar(); | |
A ab = new B(); | |
ab.Foo(); | |
ab.Bar(); | |
System.Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The output of the above is:
A::Foo
A::Bar
B::Foo
B::Bar
A::Foo
B::Bar