Books of .Net/Interview Questions Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Sunday, March 12, 2017

What is Virtual Function


Virtual Function
A virtual method is a method that can be redefined in derived classes. 
A virtual method has an implementation in a base class and may have  implementation in derived the class.We make virtual function when derived class wants to call function of base class,redefine it and later on base class can see function of derived class.

A virtual function is a member function that you expect to be redefined in derived classes. When you refer to a derived class object using  reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.
You declare a method as virtual inside a base class by preceding its declaration with the keyword virtual. When a virtual method is redefined by a derived class, the override modifier is used. Thus, the process of redefining a virtual method inside a derived class is called method overriding


When overriding a method, the name, return type, and signature of the overriding method must be the same as the virtual method that is being overridden. Also, a virtual method cannot be specified as static or abstract.



When a method is declared as a virtual method in a base class then that method can be defined in a base class and it is optional for the derived class to override that method. Derived class may or may not override that method.
It is not necessary to override a virtual method.If a derived class does not provide its own version of a virtual method, then the one in the base class is used.

Example-

Step 1- I have made a class 'Shape'. It has virtual method 'Draw'.

public class Shape

    {

        public virtual void Draw()

        {

            Console.WriteLine("Drawing Base class");

        }
    }

Step 2- I have 2 class 'Circle' and 'rectangle'. 'Circle' class has overridden virtual method Draw but 'rectangle' class have not overridden virtual method as it is not necessary to override virtual method.
class Circle : Shape
   {
       public override void Draw()
       {
           Console.WriteLine("Drawing a circle Derived class");
       }
   }
   class Rectangle : Shape
   {
       public void color()
       {
           Console.WriteLine("Coloring rectangle derived class");
       }
   }

Step 3
class Program
    {
        static void Main(string[] args)
        {
            Shape sh = new Circle();
            sh.Draw();
            Shape ss = new Rectangle();
            ss.Draw();
            Rectangle rec = new Rectangle();
            rec.color();
            Console.ReadLine();
        }
    }

Output
Drawing a circle Derived class  -1 
Drawing Base class                    -2
Coloring rectangle derived class -3

for -1 , As we passing object of derived class to reference of base class and derived class has overridden virtual method of base class,we are getting output from derived class.

for -2,As we passing object of derived class to reference of base class and derived class has not overridden virtual method of base class,we are getting output from base class.

Screenshot


















You may also like.

No comments :

Post a Comment