Virtual and Override Property in C# – What it is & How to Use

What is Virtual Property in C#?

If you are new to OOP(Object Oriented Programming) and want to know how to use virtual and override property in C#, then congrats, you’ve landed on the right article.🛬

We use a virtual keyword in the base class to modify a property, method, indexer, or event declaration, which is later overridden in a derived class. If you are having a problem understanding this concept, then don’t worry. In this article, we will discuss in detail virtual and override properties. 

Before we dive deep into this article, first, let’s discuss the two main concepts Polymorphism and method overriding. So without wasting any time, let’s start moving toward the article.

 

 

What is Polymorphism?

One of the main features of OOP(Object Oriented Programming) is polymorphism which means having more than one form. Polymorphism is also expressed as a method that performs different tasks. In inheritance, we inherit different fields and functions from another class, and in polymorphism, we use those functions to perform different operations. 

We have two types of polymorphism that are:

  1. Compile-Time Polymorphism.
  2. Run-Time Polymorphism.

Compile-time polymorphism is also known as static polymorphism, and run-time polymorphism is known as dynamic polymorphism. In run-time polymorphism, we have virtual and overriding properties. Run-time polymorphism is implemented by virtual functions. We achieve dynamic polymorphism with the help of method overriding.

 

 

What is Method Overriding?

If the same method is available in both base and derived classes. Then, the method in the derived class overrides the method in the base class is known as method overriding. The method overriding is basically the same function performed with different base and derived class operations. We can achieve method overriding with the help of virtual and override keywords.

We use a virtual keyword with a method to tell the compiler this is a method that can be overridden. The virtual keyword is always used with the parent class method, and the override keyword is used with the child class.

 

Code

//Example of virtual and override keywords

using System;




class parentClass {




// Use virtual keyword with print method

public virtual void print()

{

Console.WriteLine("Parent class");

}

}





// class parentClass inherit class child

class child : parentClass

{

//Use override with the print method of child class

public override void print()

{

Console.WriteLine("Child class");

}

}




class mainClass {

public static void Main()

{

parentClass myobj;




myobj = new parentClass();

// call the print function of parent class

obj.print();




obj = new child();

// call the print function of child class

obj.print();

}

}

 

Output

Parent class

Child class

 

 

What is a Property in C#?

We use properties to hide sensitive data from the user; for example, we declare a variable as private, provide its public access through the set and get methods through properties. We can also restrict its value with the help of get and set accessors. A property can be virtual, abstract, or override.

 Let’s understand the concept of property with the help of a simple example:

 

Code

class Student

{

 // private field or variable

  private string Sname;




 // property to access Sname  

public string StuName  

  {

// get method read Sname

    get { return Sname; }   




// set method assign value to Sname

    set { Sname = value; }    }

}

 

 

What is a Virtual Property in C#?

Virtual properties are just like virtual methods. A property with a virtual keyword is called virtual property, which allows the derived class to override it. When we read or write a virtual property through an object reference, the object’s run-time type is used to determine which implementation of the property to use. So now we can write an example in which we declare virtual property:

 

Code

class Person

{

 // private field or variable

  private string _name;




 // setting the Person’s Name property as virtual, which can be overridden

public virtual string Name  

  {

// get method return _name value

    get { return _name; }   




// set method assign value to _name

    set { _name = value; }    }

}

 

 

What is An Override Property in C#?

We use the override keyword to override a base class property, which means redefining or modifying the base class virtual property. Now let’s see an example in which we use the above-stated class as a base class which is overridden by our class.

 

Code

class Student : Person

{

 // private field or variable

  private string _name;

  private string _lname;




 // Overriding Name property of person class

public override string Name  

  {

// get method return _name value

    get { return _name; }   




// set method assign value to _name

    set { _name = value; }    }




// Declare a lastName property of type string:

      public string Code {

         get {

            return _lname;

         }

         set {

            _lname= value;

         }

      }

}

 

Now let’s understand virtual and override property with the help of a complete example:

 

Code

using System;


    class Person

{

 // private field or variable

  private string _name;




 // setting the Person’s Name property as virtual, which can be overridden

public virtual string Name  

  {

    // get method return _name value

    get { return _name; }   




    // set method assign value to _name

    set { _name = value; }    }

    

    // A virtual method to print the value of the name on the screen

    public virtual void Print()

       {

          Console.WriteLine("Name:" + Name);

       }

}




class Student : Person

{

 // private field or variable

  private string _name;

  private string _lname;




 // Overriding Name property of person class

public override string Name  

  {

// get method return _name value

    get { return _name; }   




// set method assign value to _name

    set { _name = value; }    }




// Declare a lastName property of type string:

      public string Lname {

         get {

            return _lname;

         }

         set {

            _lname= value;

         }

      }

      

      // Override the virtual function of the base class

public override void Print()

       {

          Console.WriteLine("Name:" + Name + " " + Lname);

       }

}




    class Myclass

    {

       static void Main(string[] args)

       {

          Person bObj = new Person();

          

          bObj.Name="Zeshan";

          bObj.Print();

          Student dObj = new Student();

          dObj.Name="Zeshan";

          dObj.Lname="Afridi";

          dObj.Print();




          Console.WriteLine("\nProgram Execute Successfully \n Press Any Key to Exit..");

          Console.ReadLine();

       }

    }

 

Output

Name: Zeshan

Name: Zeshan Afridi



Program Execute Successfully 

Press Any Key to Exit.

 

 

Conclusion

To conclude the article on virtual and override property in C#, we have seen that virtual property work like virtual methods. We use virtual and override keywords with property to avail feature of polymorphism. 

We write virtual with the property in the base class to which we want to be overridden in the derived class. This article has coded the simplest examples to explain virtual property in detail.

Let’s have a quick review of the topics discussed in this article.

  1. What is polymorphism?
  2. What is method overriding?
  3. What is a property in C#?
  4. What is a virtual property in C#?
  5. What is the override property in C#?

If this article is helpful and solves the mystery of virtual property, don’t forget to share it with your coding partners—also, comment below 👇. Do you understand virtual property? 

 Happy coding! 🥳

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Total
0
Share