Berat Emre Nebioglu
Berat Emre Nebioglu

Reputation: 49

virtual and override how to use those?

hello i wrote two simple classes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kus
{
    class Program : Class1
    {
        public Program()
        {}
        public override void findtheflyingdistance(out int x)
        {   x=0;
            Random r = new Random();
            for (int i = 0; i < 100; i++)
            {
                int z = r.Next(100, 500);
                x += x + z;
            }
            Console.WriteLine("Kuş" + x);

        }
        static void Main(string[] args)
        {
            int x;
            Program pg = new Program();
            pg.findtheflyingdistance(out x);
            Class1 C1 = pg;
            C1.findtheflyingdistance(out x);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kus
{
    class Class1
    {
        public Class1(){}
        public virtual void findtheflyingdistance(out int x)
        {   x=0;
            int z=200;
            x += x + z;
            Console.WriteLine("Leylek" +x);

        }
    }
}

as i understand override method override the virtual method. When i create two instance in the main Program class. findtheflyingdistance(out int x) in Program class worked for two instance but when i did method findtheflyingdistance(out int x) virtual in two clasess. Each instance work with own classes. So i dont understand the logic behind the scene. Why do we write virtual methods? if we want to override a method we can do override and non-virtual method that will be overriden already.

Upvotes: 0

Views: 161

Answers (1)

jbutler483
jbutler483

Reputation: 24529

Virtual allows you to override a method which is defined in the base class. In other words, it extends the implementation of the method in the derived class.

this would be an interesting read.

this stackoverflow answer also gives a short example, which i have copied below for handiness:

The virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it: Use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier.

This is all to do with polymorphism. When a virtual method is called on a reference, the actual type of the object that the reference refers to is used to decide which method implementation to use. When a method of a base class is overridden in a derived class, the version in the derived class is used, even if the calling code didn't "know" that the object was an instance of the derived class. For instance:

public class Base { public virtual void SomeMethod() { } }

public class Derived : Base { public override void SomeMethod() { } }
  ...

 Base b = new Derived(); 
b.SomeMethod(); 

will end up calling Derived.SomeMethod if that overrides Base.SomeMethod.

Now, if you use the new keyword instead of override, the method in the derived class doesn't override the method in the base class, it merely hides it. In that case, code like this:

 public class Base { public virtual void SomeOtherMethod() { } }

 public class Derived : Base { public new void SomeOtherMethod() { } }

 ...


 Base b = new Derived();
 Derived d = new Derived();
 b.SomeOtherMethod(); 
d.SomeOtherMethod();

Will first call Base.SomeOtherMethod , then Derived.SomeOtherMethod . They're effectively two entirely separate methods which happen to have the same name, rather than the derived method overriding the base method.

If you don't specify either new or overrides, the resulting output is the same as if you specified new, but you'll also get a compiler warning (as you may not be aware that you're hiding a method in the base class method, or indeed you may have wanted to override it, and merely forgot to include the keyword).

An overriding property declaration may include the sealed modifier. Use of this modifier prevents a derived class from further overriding the property. The accessors of a sealed property are also sealed.

Upvotes: 1

Related Questions