Wednesday, March 29, 2017

Virtual vs Override vs New Keywords in C#

Virtual Keyword

Virtual keyword is used for generating a virtual path for its derived classes on implementing method overriding. Virtual keyword is used within a set with override keyword. It is used as:
Hide   Copy Code
// Base Class
    class A
    {
        public virtual void show()
        {
            Console.WriteLine("Hello: Base Class!");
            Console.ReadLine();
        }
    }

Override Keyword

Override keyword is used in the derived class of the base class in order to override the base class method. Override keyword is used with virtual keyword, as:
Hide   Copy Code
// 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

New keyword is also used in polymorphism concept, but in the case of method overloading So what does overloading means, in simple words we can say procedure of hiding your base class through your derived class.
It is implemented as:
Hide   Copy Code
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();
        }
    }

Demo Example

Sample Implementation

Here’s a simple implementation in C# without using any keyword. Do you think it will run fine or show some RUN or COMPILE time errors?
Let’s see:
Hide   Shrink Description: https://www.codeproject.com/images/arrow-up-16.png   Copy Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Generics
{
    class A
    {
        public void show()
        {
            Console.WriteLine("Hello: Base Class!");
            Console.ReadLine();
        }
    }
 
    class B : A
    {
        public void show()
        {
            Console.WriteLine("Hello: Derived Class!");
            Console.ReadLine();
        }
    }
 
    class Polymorphism
    {
        public static void Main()
        {
            A a1 = new A();
            a1.show();
            B b1 = new B();
            b1.show(); 
            A a2 = new B();
            a2.show();
 
        }
    }
}

Output Window

Description: https://www.codeproject.com/KB/cs/816448/image001.jpg
It is showing some sort of output which means there is neither run time nor compile time error. But it will definitely show a Warning to you in your Visual Studio. So what is it and how to remove it. Do you want to know?
Keep reading and you will go through that.

Warning Message

Here’s a warning message that you will get:
Description: https://www.codeproject.com/KB/cs/816448/image002.jpg

Solution

The solution of this problem is already in the warning. Just read it carefully and you will get that. Yes, you got that right, for removing that warning we need to use NEW keyword.
In the next sample, the demo example shows you a simple demo snippet and implementation of new keyword.
(I hope now you get why we are using new keyword in here.)

New Keyword | Method Overloading

Here’s a simple snippet of method overloading mechanism. So just go through it and guess the output:
Hide   Shrink Description: https://www.codeproject.com/images/arrow-up-16.png   Copy Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Generics
{
    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();
        }
    }
 
    class Polymorphism
    {
        public static void Main()
        {
            A a1 = new A();
            a1.show();
            B b1 = new B();
            b1.show(); 
            A a2 = new B();
            a2.show();
 
        }
    }
}

Output Window

Description: https://www.codeproject.com/KB/cs/816448/image001.jpg

Explanation

The procedure goes something like this:
(using overhiding as a ref for overloading)
Description: https://www.codeproject.com/KB/cs/816448/image003.gif

Virtual & Override Keywords | Method Overriding

This is a simple example of method overriding. Just go through it and guess the output again and also try to differentiate between the previous snippet and this snippet.
Hide   Shrink Description: https://www.codeproject.com/images/arrow-up-16.png   Copy Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Generics
{
    class A
    {
        public virtual void show()
        {
            Console.WriteLine("Hello: Base Class!");
            Console.ReadLine();
        }
    }
 
    class B : A
    {
        public override void show()
        {
            Console.WriteLine("Hello: Derived Class!");
            Console.ReadLine();
        }
    }
 
    class Polymorphism
    {
        public static void Main()
        {
            A a1 = new A();
            a1.show();
            B b1 = new B();
            b1.show();
            A a2 = new B();
            a2.show();
        }
    }
}

Output Window

Description: https://www.codeproject.com/KB/cs/816448/image004.jpg

Explanation

The flow goes something like this:
Description: https://www.codeproject.com/KB/cs/816448/image005.gif

Overriding + Hiding | Together

In this snippet, I show how both these methods can work together in the same snippet. So just go through this and guess the output.
Hide   Shrink Description: https://www.codeproject.com/images/arrow-up-16.png   Copy Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Generics
{
    class A
    {
        public virtual void show()
        {
            Console.WriteLine("Hello: Base Class!");
            Console.ReadLine();
        }
    }
 
    class B : A
    {
        public override void show()
        {
            Console.WriteLine("Hello: Derived Class!");
            Console.ReadLine();
        }
    }
 
    class C : B
    {
        public new void show()
        {
            Console.WriteLine("Am Here!");
            Console.ReadLine();
        }
    }
 
    class Polymorphism
    {
        public static void Main()
        {
            A a1 = new A();
            a1.show();
            B b1 = new B();
            b1.show();
            C c1 = new C();
            c1.show();
            A a2 = new B();
            a2.show();
            A a3 = new C();
            a3.show();
            B b3 = new C();
            b3.show();
        }
    }
}

Output Window

Description: https://www.codeproject.com/KB/cs/816448/image006.jpg

Explanation

The flow goes something like this:
(using overhiding as a ref for overloading)
Description: https://www.codeproject.com/KB/cs/816448/image007.gif

Key Points

I am giving some key points about these keywords by taking reference of method overloading and overriding concepts, as these keywords are used in these mechanism.

Virtual & Override

·         Used in polymorphism implementation
·         Includes same method name and same params’
·         Used in method overriding concept
·         It is also called run time polymorphism
·         Causes late binding

New

·         It is also used in polymorphism concept
·         Includes same method name and different params’
·         Used in method overloading concept
·         It is compile time polymorphism
·         Cause early binding


No comments:

Post a Comment