Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we use virtual and override?

Tags:

c#

Why do we use override and virtual if it gives the same effect when we dont use override and virtual?

example 1:

class BaseClass
{
    public virtual string call()
    {
        return "A";
    }
}

class DerivedClass : BaseClass
{
    public override string call()
    {
        return "B";
    }
}

output : B

Example 2:

class BaseClass
{
    public string call()
    {
        return "A";
    }
}

class DerivedClass : BaseClass
{
    public string call()
    {
        return "B";
    }
}

and the output is still the same:

output : B

to run the test:

class Program
{
    static void Main(string[] args)
    {
        DerivedClass dc = new DerivedClass();
        Console.WriteLine(dc.call());
        Console.ReadKey();
    }
}

Does the compiler add virtual and override automatically at compile time?

I would be pleased if someone would explain to me the reason for using virtual and override.

like image 946
Racooon Avatar asked Oct 24 '11 12:10

Racooon


2 Answers

(note, I'm quietly ignoring the compile errors)

Now do:

BaseClass obj = new DerivedClass();
Console.WriteLine(obj.call());

Without virtual, this will print A, when actually a DerivedClass should be writing B. This is because it has simply called the BaseClass implementation (since obj is typed as BaseClass, and no polymorphism is defined).

like image 59
Marc Gravell Avatar answered Sep 23 '22 19:09

Marc Gravell


Virtual and override are a base mechanism of inheritance in object oriented programming. This is perhaps the most important thing to understand when you use classes in a language like C# or Java.

http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)

Inheritance allow you to reuse code adding new fields, properties and methods or replacing methods and properties of previously defined classes.

Virtual and Override allow you to replace the content of a method, and when i say replace, i say replace.

I would propose you a nice example.

public class MyClassEnglish
{
    public virtual string SomethingToSay()
    {
        return "Hello!";
    }

    public void WriteToConsole()
    {
        Console.WriteLine(this.SomethingToSay());
    }
}

public class MyClassItalian :
    MyClassEnglish
{
    public override string SomethingToSay()
    {
        return "Ciao!";
    }
}

int main()
{
    MyClassItalian it = new MyClassItalian();

    it.WriteToConsole();
}

If you omit virtual and override, MyClassItalian will print out "Hello!" and not "Ciao!".

In your example you show a Shadowing technique, but the compiler should give you a warning. You shoul add the "new" keyword if you want to hide a method in a base class. Hiding a method is not overriding! Is just hiding.

One possible use that comes into my mind is that it can be used when you need some kind of optimization for example.

public abstract class MySpecialListBase
{
    public int Count()
    {
        return this.GetCount();
    }

    protected abstract int GetCount();
}

public sealed class MySpecialArrayList : MySpecialListBase
{
    int count;

    public new int Count()
    {
        return this.count;
    }

    protected override int GetCount()
    {
        return this.count;
    }
}

Now... You can use MySpecialListBase in all your code, and when you call the Count() it will call the virtual method GetCount(). But if you use just MySpecialArrayList it will call the optimized Count() that is not virtual and that just return a field, increasing performances.

// This works with all kind of lists, but since it is a more general purpose method it will call the virtual method.    
public void MyMethod(MySpecialListBase list)
{
    Console.WriteLine(list.Count());
}

// This works only with MySpecialArrayList, and will use the optimized method.
public void MyMethod(MySpecialArrayList list)
{
    Console.WriteLine(list.Count());
}
like image 36
Salvatore Previti Avatar answered Sep 23 '22 19:09

Salvatore Previti