The purpose of writing this article is to explain the basic differences between the frequently used keywords in C#. This article is mainly targeted for the beginner to .NET technologies.
Overview
In this article I am going to explain how virtual, override and new keywords vary by taking some sets of examples respectively.
Virtual Keyword
Virtual keyword is used for generating a virtual path for its derived classes on implementing method overriding. The Virtual keyword is used within a set with an override keyword. It is used as:
- class Parent
- {
- public virtual void SayHello()
- {
- Console.WriteLine("Hello from Parent function!!!");
- Console.ReadLine();
- }
- }
Override Keyword
Override keyword is used in the derived class of the base class in order to override the base class method. The Override keyword is used with the virtual keyword, as in:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace NewVirtualOverride
- {
-
- class Parent
- {
- public virtual void SayHello()
- {
- Console.WriteLine("Hello from Parent function!!!");
- }
- }
-
- class Child : Parent
- {
- public override void SayHello()
- {
- Console.WriteLine("Hello from Child class function!!!");
-
- }
- }
-
- class Program
- {
- static void Main(string[] args)
- {
- Parent pObj = new Parent();
- pObj.SayHello();
- Child cObj = new Child();
- cObj.SayHello();
- Parent pcObj = new Child();
- pcObj.SayHello();
-
-
- Console.ReadLine();
- }
- }
- }
New Keyword
New keyword is used for the runtime polymorphism (method overriding), we can change the base class function with derived class function.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace NewVirtualOverride
- {
-
- class Parent
- {
- public void SayHello()
- {
- Console.WriteLine("Hello from Parent function!!!");
- }
- }
-
- class Child : Parent
- {
- public new void SayHello()
- {
- Console.WriteLine("Hello from Child class function!!!");
-
- }
- }
-
- class Program
- {
- static void Main(string[] args)
- {
- Parent pObj = new Parent();
- pObj.SayHello();
- Child cObj = new Child();
- cObj.SayHello();
- Parent pcObj = new Child();
- pcObj.SayHello();
-
-
- Console.ReadLine();
- }
- }
- }
In the above example if you don't specify the ‘new’ keyword it will give warning message as we have already specified the same method name in the parent class, but it will execute the child class method by default.
Key Points
Below are the key points on new, virtual, override keywords.
Virtual and Override
- Used in polymorphism implementation (method overriding)
- It is also called runtime polymorphism
- Includes same method name and same signatures
- Causes late binding
New
- It is also in polymorphism implementation (method overriding)
- Includes the same method name and different signatures
- It is compile-time polymorphism
- Causes early binding
Hope this article will help you in understanding how to implement runtime polymorphism and also concepts of late and early binding.
Read more articles on C#: