Friday, 5 September 2014

How to call a base class method even derived class object is created.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InheritenceExample
{
class Program
{
static void Main(string[] args)
{
b obja = new b();
obja.Method1();
Console.ReadLine();
}
}


public class a
{
public virtual void Method1()
{
Console.WriteLine("base class method is called.");
}
}

public class b : a
{
public override void Method1()
{
base.Method1(); // calling base class method.
}
}

}
Result :  Base class method is called.

No comments:

Post a Comment