Explain Virtual Keyword & Overriding in OOPs

Explain Virtual Keyword & Overriding in OOPs

Virtual Keyword & Overriding

Virtual Methods
By declaring a base class function as virtual, you allow the function to be overridden in any derived class.
The idea behind a virtual function is to redefine the implementation of the base class method in the derived class as required.

Override Keyword
We must include the override keyword in the derived class if a method is virtual in the base class.
Overrides the functionality of a virtual method in a base class, providing different functionality.

If a method with the same method signature is presented in both child and parent class is known as method overriding.
The methods must have the same number of parameters and the same type of parameter. It overrides the value of the parent class method.
It is also known as runtime polymorphism.

class Printer
    public virtual void display()
        Console.WriteLine("Display dimension: 5x5");
    public virtual void print()
        Console.WriteLine("Printer printing...\n");

class LaserJet : Printer
    sealed override public void display()
        Console.WriteLine("Display dimension: 10x10");
    override public void print()    
        Console.WriteLine("LaserJet Printer printing...\n");

class OfficeJet : LaserJet
    //Can not override display function or else
    //compiler error : 'OfficeJet.display()': cannot override inherited member
    //'LaserJet.display()' because it is sealed.

    //override public void display()
    //    Console.WriteLine("B.F");
    override public void print()
        Console.WriteLine("OfficeJet Printer printing...");


Virtual vs Sealed Methods
Virtual methods allow for flexibility in modifying the behavior of a class through inheritance, while sealed methods provide a stronger level of encapsulation and protection against unintended modifications.
In terms of performance, virtual method calls add overhead to the performance, as the runtime must determine the correct implementation of a method to call at runtime. On the other hand, sealed methods do not have virtual method calls, which results in a performance boost.
We cannot override a sealed method

How Virtual Works in C#?
The very basic difference between overloading and overriding is that the former is a compile-time mechanism, while the latter comes into play at run time. A virtual keyword comes into action at runtime, thus implementing the method overriding concept.
When any virtual method or property is invoked or accessed, the compiler checks for an overriding member of the method or the property. If one is found, it is invoked. If none is found, the original method or property is invoked.
In case of multilevel inheritance, the deepest overridden member is invoked (the one in the most derived class).

A variable cannot be declared virtual. Only properties, methods and events can be declared as virtual.
A static member cannot be declared virtual.
An abstract member cannot be declared virtual.
A private member cannot be declared virtual.
A non-virtual member cannot be overridden.
The access level, type, and name of both virtual members and the overriding members must be the same. For e.g., if the virtual method is public, overriding method must also be public.

Abstarct Methods Vs Virtual Methods
1. Abstract Method resides in an abstract class only and it has no body. Virtual Method can reside in abstract and non-abstract class.
2. Abstract Method must be overridden in non-abstract child class. It is not necessary to override virtual method in derived but it can be.
3. A Virtual method must always have a default implementation. However, it can be overridden in a derived class by the keyword override.
An Abstract method doesn’t have any implementation. It resides in the abstract class, and also it is mandatory that the derived class must implement abstract class. Use of override keyword is not necessary.

Overriding Vs Hiding Methods
If a method with the same signature is declared in both base and derived classes, but the methods are not declared as virtual and overridden respectively, then the derived class version is said to hide the base class version.

Related Post

About Us

Community of IT Professionals

A Complete IT knowledgebase for any kind of Software Language, Development, Programming, Coding, Designing, Networking, Hardware and Digital Marketing.