In one case you are doing method hiding and in another you are overriding it.Don't hesitate to take help of google,you'll get very useful articles or answers.Even on SO,you'll get many similar questions.
class A
{
public void Display()
{
Console.WriteLine("A's Method");
}
}
class B : A
{
public void Display()
{
Console.WriteLine("B's Method");
}
}
In the above example,you are doing shadowing or method hiding.You can get the even the same result if you use new keyword like in below code.if you use don't write override in method of child class, the method in the derived class doesn't override the method in the base class, it merely hides it.
public new void Display()
{
Console.WriteLine("B's Method");
}
The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.So to aceive overriding you have to use override keyword which you are using in second example.
Simply put, if a method is not overriding the derived method, it is hiding it.An override method provides a new implementation of a member that is inherited from a base class.
You can also check here on MSDN when to do method hiding or when to do overriding.
Based on discussion on comment section,i am attaching below code.Hope it'll help you.
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
A a=new A();
a.Display();//Parent's Method
B b =new B();
b.Display();//Child's Method
**A ab = new B();
ab.Display();//Parent's Method**
Console.ReadKey();
Parent parent = new Parent();
parent.Display();//Parent's Method
Child child = new Child();
child.Display();//Child's Method
**Parent ParentChild = new Child();
ParentChild.Display();//Child's Method**
Console.ReadKey();
}
class A
{
public virtual void Display()
{
Console.WriteLine("Parent's Method");
}
}
class B : A
{
public void Display()
{
Console.WriteLine("Child's Method");
}
}
class Parent
{
public virtual void Display()
{
Console.WriteLine("Parent's Method");
}
}
class Child : Parent
{
public override void Display()
{
Console.WriteLine("Child's Method");
}
}
}
}