using System;
namespace np
{
class program
{
static void Main()
{
contact c1=new class1();
contact c2=new class2();
c1.printf();
c2.printf();
Console.ReadKey();
}
}
public abstract class contact
{
public virtual void printf()
{
Console.WriteLine("This is virtual method.contact");
}
}
public class class1:contact
{
public override void printf(){
Console.WriteLine("this is override Method class1!");
}
}
public class class2:class1
{
public new void printf()
{
Console.WriteLine("this is new method. class2");
}
}
}
the result of this code:
this is override Method class1!
this is override Method class1!
New : hide the method of the Parent class.but if set a subclass to a parent(Polymorphism),when call the variable's method ,the parent's method will be called.
override: overwrite the parent 's method or properties.in Polymorphism, the subclass's method will be called.