What is the purpose of virtual keyword in .NET?

Virtual keyword means that the method or property or indexer or declaration of an event associated with virtual keyword in the base class can be overridden by the derived class. Here is an example to demonstrate the usage of virtual keyword to override a method:



class baseEmployee {
public float empSalary;
public virtual void assignSalary(float amt) {
empSalary = amt;
}
public void displaySalary() {
Console.WriteLine(“Salary = {0}”, empSalary);

}
}

class derivedSupervisor : baseEmployee {
public float supIncentive = 3000;
public override void assignSalary(float amt) {
empSalary = amt + 3000;
}
}

class derivedLabor:baseEmployee {
public override void assignSalary(float dailyWage) {
empSalary = dailyWage * 30;
}
}

class testClass {
Console.WriteLine(“Executing baseEmployee …”);
baseEmployee bObj = new baseEmployee();
bObj.assignSalary(10000);
bObj.displaySalary();
Console.WriteLine(“Executing derivedSupervisor …”);
bObj = new derivedSupervisor();
bObj.assignSalary(10000);
bObj.displaySalary();
Console.WriteLine(“Executing derivedLaborObject…”);
bObj = new derivedLabor();
bObj.assignSalary(100);
bObj.displaySalary();
}
Output of this code will be:
Executing baseEmployee …
Salary = 10000
Executing derivedSupervisor …
Salary = 13000
Executing derivedLaborObject…
Salary = 3000

In this example, you have defined a virtual method called assignSalary in the base class which is used to assign salary for the employee based on the amount that is passed as argument. This base class is derived by two derived classes namely derivedSupervisor, derivedLabor. These derived classes are allowed to override assignSalary method of baseEmployee because the method is defined as virtual. While overriding, these two derived classes include their own computation method for assigning salary.

Note that if the keyword virtual is not mentioned in assignSalary of baseEmployee and you define the same method in the derived classes, then it is not overriding. The method definitions in derived class are considered as new method that is independent of the base class method.

| How do you prevent a class from overriding in .NET? | How are classes related to objects in .NET Application | How are Delegates different from Events in .NET? | How are system exceptions different from application exceptions in .NET? | How are Value Types different from Reference Types in .NET? | How can a finalize method be suppressed in .NET? | How can you call Stored Procedure in ADO.NET? | How can you force Dispose method to be called automatically in .NET? | How do you call a Base Class Constructor from Derived Class Constructor in .NET? | How do you connect your VB.NET application to SQL Server? | How do you implement Cloning in .NET? | How do you implement Façade Design Pattern in .NET? | How do you implement MVC Pattern in ASP.NET? | How do you install .NET Assembly in GAC? | How is shadowing different from overriding in .NET? | How to prevent a particular .NET DLL from being decompiled? | Illustrate Delay Signing Process of an Assembly in .NET? | What are Reference Types in .NET? | What are the advantages of C#? | What are the advantages of VB.NET? | What are the differences between Namespace and Assembly in .NET? | What are the similar features between class and structure in .NET? | What are Value Types in .NET? | What do you mean by mixed mode authentication in .NET? | What do you mean by Satellite Assembly in .NET? | What do you mean by shadowing in .NET? | What is CTS in .NET? | What is ILDASM in .NET? | What is Managed Code in .NET? | What is Manifest in .NET? | What is MSIL in .NET Framework? | What is the importance of finalize method in .NET? | What is the need for Visitor Pattern in C#? | What is the purpose of bindingRedirect tag in web.config file of .NET? | What is the purpose of CodeDom in .NET? | What is the purpose of dispose method in .NET? | What is the purpose of Ngen.exe in .NET? | What is the purpose of Strong Name in COM Components of .NET? | What is the purpose of virtual keyword in .NET? | What Object Oriented Principles can be incorporated in .NET Application? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.