Create a Console Application and write the following code...
class Program
{
public class BaseClass // Base class
{
public virtual void GetData() // by defining Virtual, the method is going to override in Derived class
{
System.Console.WriteLine("From Base Class");
}
}
public class DerivedClass : BaseClass
{
public override void GetData() // The method has been over riden in the derived class with Override keyword
{
System.Console.WriteLine("From Derived Class");
}
}
static void Main(string[] args)
{
DerivedClass objDerive = new DerivedClass();
objDerive.GetData();
Console.Read();
}
}
If the method is not defined with Virtual, we can over ride method with NEW keyword
Example :-
class Program
{
public class BaseClass
{
public void GetData()
{
System.Console.WriteLine("From Base Class");
}
}
public class DerivedClass : BaseClass
{
public new void GetData() // the method we have written with NEW keyword
{
System.Console.WriteLine("From Derived Class");
}
}
static void Main(string[] args)
{
DerivedClass objDerive = new DerivedClass();
objDerive.GetData();
Console.Read();
}
}
No comments :
Post a Comment