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.