10
Reply

What is Virtual keyword?

Samir Bhogayta

Samir Bhogayta

Sep 03, 2015
1.8k
0

    We use Virtual keyword in the base class method so that we can achieve Runtime polymorphism. or you can better say Late Binding. you can override the base class method in the derived class.

    Deeksha Pandit
    June 05, 2016
    4

    If a base class method is to be overriden, it is defined using the keyword virtual

    Ravi Patel
    October 18, 2016
    2

    when member functions in both base and derived class is having same function name then the function in base class declaired with the keyword "virtual".

    Barkha Gupta
    January 22, 2016
    2

    virtual keyword is used for achieving method overriding. in derived class, we want to override the base class method then it should be declared virtual in base class

    Rahul Sharma
    October 18, 2016
    1

    Nice explanation Anil Kumar Murmu.

    Prakash Tripathi
    April 10, 2016
    1

    @ Barkha Gupta. Having the same name in base and derived is the result of thought process. However the main thought is if we want any method to be overridden in child classes, then we declare the method as virtual.

    Prakash Tripathi
    April 10, 2016
    1

    using virtual keyword member can be overriden in a child class.

    Keerthi Venkatesan
    April 04, 2016
    1

    Let us understand what virtual in layman term means. It means "not physically existing". Now lets assume one of the scenario, if we have a function say Add(int firstNumber, int secondNumber); it is present both in parent class as well as child class. Now the rules in inheritance says if we create an instance of Child class, it has access to all public data members/functions in Child class as well as parent class. Now if we compile this code in VS, then we will get a warning to use New key word in Child class Add() method definition. This is the concept of method hiding. However, if suppose we remove the implementation of Add method in child class then it will call the Parent class method. That is why we should use Virtual keyword. Again we have to bid with few of OOPs rules: if any method is virtual in parent class then in child class while implementing the same(which is not mandatory) should be marked as override.

    Anil Kumar Murmu
    February 05, 2016
    1

    This keyword indicates that a member can be overridden in a child class. It can be applied to methods, properties, indexes and events.

    Samir Bhogayta
    September 03, 2015
    1

    The "Virtual" keyword means that the method and variable are marked as virtual in base class you can override in derived class and change the definition of that method, its useful when you perform the Late Binding (Dynamic Binding).

    Alok Tripathi
    March 29, 2017
    0