2

I just have one basic question :

public class virtualTest
   {
       public virtual void vTest()
       {
           Console.WriteLine("Base Class");
       }
   }

   public class derivedVirtualTest : virtualTest
   {
       public override void  vTest()
        {
            Console.WriteLine("Derived Class");
        }
   }

Here i have used function overriding with function vTest()

But if i :

public class virtualTest
   {
       public void vTest()
       {
           Console.WriteLine("Base Class");
       }
   }

   public class derivedVirtualTest : virtualTest
   {
       public void  vTest()
        {
            Console.WriteLine("Derived Class");
        }
   }

removes virtual and override keywords from respective places , then also code works.

How can this be possible?

Or then what is the use of override and virtual (entire function overriding) if code works fine without virtual and override???

EDIt:

My Method through which i am calling above classes

 static void Main(string[] args)
        {



            derivedVirtualTest objderivedVirtualTest = new derivedVirtualTest();
            objderivedVirtualTest.vTest();

            virtualTest objvirtualTest = new virtualTest();
            objvirtualTest.vTest();

            Console.ReadLine();


        }
C Sharper
  • 8,284
  • 26
  • 88
  • 151
  • You said this code works, but you never shown how you tested it. Post that code also. That is the most important part. – Sriram Sakthivel Nov 05 '14 at 06:45
  • @SriramSakthivel just posted. Please Have a look at it – C Sharper Nov 05 '14 at 06:48
  • Refer [this](http://stackoverflow.com/questions/392721/difference-between-shadowing-and-overriding-in-c) To use polymorphism you need to have compile time type as basetype not derived type. Just change the `derivedVirtualTest objderivedVirtualTest` to `virtualTest objderivedVirtualTest ` to see the difference. – Sriram Sakthivel Nov 05 '14 at 07:00
  • read this http://msdn.microsoft.com/en-us/library/ms173153.aspx – Hamid Pourjam Nov 05 '14 at 07:46

3 Answers3

6

As qwr explained, the main difference in terms of OOP is polymorphism. It means that if you access the class which overrides the base member through a base type reference, the call you perform to the overriddable member is redirected to the override.

In case of a class which shadows/hides the base member, the call is not redirected, and the base class' method is being executed.

So, given:

class Base
{
    public virtual void OverrideMe()
    {
        Console.WriteLine("I'm the base");
    }
}

class Derived : Base
{
    public override void OverrideMe()
    {
        Console.WriteLine("I'm derived");
    }
}

class Shadowing : Base
{
    public void OverrideMe()
    {
        Console.WriteLine("I'm shadowing");
    }
}

And using them this way:

var instances = new Base[] {new Base(), new Derived(), new Shadowing()};

foreach (var instance in instances)
{
    instance.OverrideMe();
}

Will produce:

I'm the base

I'm derived

I'm the base

Additional difference is that in case of overriding you can evolve your base class, like changing the signature of the base member or removing it completely, without changing the hiding one. Which in some cases may suit needs exactly and in some - not so much.

In case of overriding you must change the signature of overriding member as well, otherwise your code will fail to compile.

galenus
  • 2,087
  • 16
  • 24
  • suppose in above example there are only two classes : Base and Shadowing and OverrideMe is not declared as virtual in Base. Then is it called shaddowing ???? – C Sharper Nov 05 '14 at 07:20
  • 1
    Yes. And the derived method will never be called if you reference the derived class through the *Base* type. In addition, you can even make the hiding method return some value (not *void*) and compiler will not produce an error, only warning for not using the *new* keyword. – galenus Nov 05 '14 at 07:32
3

In the second example, maybe you test your code like this:

derivedVirtualTest deviTest = new derivedVirtualTest();
deviTest.vTest();  //Result "Derived Class"

Try this, and you'll see that the function has't been overridden yet:

virtualTest deviTest = new derivedVirtualTest();
deviTest.vTest();  //Result "Base Class"
//This will result "Dervived class" in the first one
Huy Hoang Pham
  • 4,107
  • 1
  • 16
  • 28
1

It will be needed when your sealed method overriding a method

public class Animal{  
    public virtual void eat() { Console.WriteLine("eating..."); }  
    public virtual void run() { Console.WriteLine("running..."); }  
  
}  
public class Dog: Animal  
{  
    public override void eat() { Console.WriteLine("eating bread..."); }  
    public sealed override void run() {   //If you skipped override here it will throws an error
    Console.WriteLine("running very fast...");   
    }  
}  
public class TestSealed  
{  
    public static void Main()  
    {  
        BabyDog d = new BabyDog();  
        d.eat();  
        d.run();  
    }  
}  
sohaibali5
  • 11
  • 2