C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
.NET
ADO.NET
Android
ASP.NET
C#
Databases & DBA
Design Patterns & Practices
iOS
Java
OOP/OOD
SharePoint
Software Testing
Web Development
WPF
View All
10
Reply
What is Virtual keyword?
Samir Bhogayta
10y
1.9k
0
Reply
Delete Row
Delete Column
Insert Link
×
Insert
Cancel
Embed YouTube Video
×
Width (%)
Height (%)
Insert
Cancel
Table Options
×
Rows
Columns
First row as header
Create Table
Insert Image
×
Selected file:
Alignment
Left
Center
Right
Select an image from your device to upload
Upload to Server
Cancel
Submit
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
9y
4
If a base class method is to be overriden, it is defined using the keyword virtual
Ravi Patel
9y
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
9y
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
9y
1
Nice explanation Anil Kumar Murmu.
Prakash Tripathi
9y
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
9y
1
using virtual keyword member can be overriden in a child class.
Keerthi Venkatesan
9y
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
9y
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
10y
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
8y
0
What is Sealed modifiers?
What is New modifiers?
Message