Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function overriding without virtual and override

Tags:

c#

.net

asp.net

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();


        }
like image 534
C Sharper Avatar asked Mar 28 '26 21:03

C Sharper


2 Answers

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.

like image 86
galenus Avatar answered Mar 31 '26 10:03

galenus


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
like image 27
Huy Hoang Pham Avatar answered Mar 31 '26 11:03

Huy Hoang Pham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!