Virtual Keyword

The 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:

// Base Class 
class A 

    public virtual void show() 
    { 
        Console.WriteLine(“Hello: Base Class!”); 
        Console.ReadLine(); 
    } 

Override Keyword

The 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:

// Base Class 
class A 

    public virtual void show() 
    { 
        Console.WriteLine(“Hello: Base Class!”); 
        Console.ReadLine(); 
    } 

 
// Derived Class 
class B : A 

    public override void show() 
    { 
        Console.WriteLine(“Hello: Derived Class!”); 
        Console.ReadLine(); 
    } 

New Keyword

The New keyword is also used for polymorphism but in the case of method overriding. So what does overriding means? In simple words we can say that we are changing what the base class does for the derived class.

It is implemented as:

class A 

    public void show() 
    { 
        Console.WriteLine(“Hello: Base Class!”); 
        Console.ReadLine(); 
    } 

 
class B : A 

    public new void show() 
    { 
        Console.WriteLine(“Hello: Derived Class!”); 
        Console.ReadLine(); 
    } 
}  

Advertisements