Method Overriding in C#

By
When one class inherit from another class ,all the member of parent class become the member of child class .If there is a method of parent class that we want to predefined in child class. we can implement the concept of method overriding ,it means a method overriding of parents class and child class can have same method name with same parameter but parent class method will have "VIRTUAL"Keyword and child class method have "OVERRIDE"Keyword.


EXAMPLE:-

using System;
namespace methodoverriding
{
    class Program
    {
        static void Main(string[] args)
        {
            employee obj = new employee();
            obj.display();
            Console.ReadLine();
        }
    }
public class cls
{
    public virtual void display()
    {
        Console.WriteLine("hello");
    }
}
public class employee : cls
{
    public override void display()
    {
        Console.WriteLine("welcome");
    }
}
}

Description:- Here ,I have maked the object of employee class. The display method of only employee class will get in memory,because the "display method" of cls  class has been over-hidden by the employee class.

Follow these steps to run this program:-


Step1:- Open your visual studio and go File->New Project->Click Console Application.
See it:-



Step2:- copy the whole program code in program.cs file.
See it:-

Step3:-  Now Run the Application(Press F5).
OUTPUT:-


I hope this is helpful for you
For More:-

  1. Constructor and Destructor
  2. Array in C#

To Get the Latest  Free Updates Subscribe
Click below for download whole application.
               DOWNLOAD  

2 comments:

Powered by Blogger.