Overriding
If we create base class method to virtual and override the method of the derived class, now when we instantiate the base class object to derived class and call the method with respect to the object then method of derived class will be invoked.
Sample Code
Outputnamespace Inheritance { class Program { static void Main(string[] args) { BaseDemo bd = new DerivedDemo(); bd.PrintString(); Console.ReadLine(); } } class BaseDemo { public BaseDemo() { } public virtual void PrintString() { Console.WriteLine("Base Demo Invoked"); } } class DerivedDemo : BaseDemo { public DerivedDemo() { } public override void PrintString() { Console.WriteLine("Derived Demo Invoked"); } } }
Derived Demo Invoked
No comments:
Post a Comment