Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If derived class does not override the method,which version should be called?

I am trying understand the need of override and virtual in C#,so I wrote the following code:

using System;
namespace Override
{
    class Base 
    {
        public virtual void method() 
        {
            Console.WriteLine("Base method");
        }
    }
    class Derived : Base 
    {
        public override void method()
        {
            Console.WriteLine("Derived method");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Derived d = new Derived();
            d.method();
        }
    }
}

And I was expecting "Derived method" to be called and printed.Then I wrote the following code without using virtual/override combination.

using System;
namespace Override
{
    class Base 
    {
        public void method() 
        {
            Console.WriteLine("Base method");
        }
    }
    class Derived : Base 
    {
        public void method()
        {
            Console.WriteLine("Derived method");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Derived d = new Derived();
            d.method();
        }
    }
}

And I got the same result i.e. "Derived method" called and printed.My question is if the code worked without virtual/override as I expected,what is the need of them? or am I missing something here?

like image 584
ZoomIn Avatar asked Jul 16 '13 13:07

ZoomIn


2 Answers

In your source code, you are always doing simple inheritance without any polymorphic behavior. You are always created instance of derived class and assigning it to derived class instance variable.

DerivedClass d = new DerivedClass(); // here no polymorphism, and only inheritance is there

So When you will call method using class variable, it will always call DerivedClass method, no matter if the method is virtual or not in parent class.

In Polymorphism, your programs do not know the exact type of class on which you are calling the method (this concept is called late-binding). As in example below:

BaseClass b = new DerivedClass(); // here b is a base class instance but initiated using derived class

After calling b.method() it will do late binding and will show polymorphic behavior (only if the method has been set virtual in the base class)

NOTE: The virtual keyword delays binding to correct version of method to runtime and is core keywork to implement polyphorphism. So for exact polymorphic behavior, declare methods as virtual in parent class, and then in child class, ovverride that method.

like image 114
Irfan Avatar answered Oct 23 '22 08:10

Irfan


virtual allows the correct version of the method to be chosen at runtime, based on information not available at compile time. Consider the following tweak to your example:

using System;
namespace Override
{
    class Base 
    {
        public virtual void method() 
        {
            Console.WriteLine("Base method");
        }
    }
    class Derived : Base 
    {
        public override void method()
        {
            Console.WriteLine("Derived method");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Derived d = new Derived();
            Base b = d;
            b.method();
        }
    }
}

With virtual/override, this code will display Derived method, as at runtime we can see that b is really a Derived instance. Without virtual/override, it will display Base method, as the declared type of b is Base.

like image 42
Chowlett Avatar answered Oct 23 '22 06:10

Chowlett