Reputation: 213
I don't really understand - when am I supposed to use virtual functions?I'll be glad if someone could explain it to me, thanks.
Upvotes: 5
Views: 1487
Reputation: 19956
public abstract class Weapon
{
public virtual abstract FireOnTarget();
}
public class Pistol : Weapon
{
public override FireOnTarget()
{
LoadPowder();
LoadBullet();
CockIt();
PullTheTrigger();
}
}
public class SucideBomber : Weapon
{
public override FireOnTarget()
{
BuyTruck();
LoadWithC4();
DriveToTarget();
ActivateDetonator();
}
}
OK now you have two classes. The point is to refer to the virtual function without knowing what actual class is there, for example:
public void PerformFiring(Weapon W)
{
// do stuff
W.FireOnTarget();
// do more stuff
}
This method will use whichever object you send in, derived from Weapon, and call FireOnTarget on that object. For example:
Pistol p=new Pistol();
PerformFiring(p);
Upvotes: -1
Reputation: 797
virtual functions are ones that a subclass can override if it wishes
//in parent
public virtual string someMethod()
{
return "someting";
}
//in child
public override string someMethod()
{
return "someting else";
}
Upvotes: 1
Reputation: 21742
probably easiest to understand through an example So imagin we have code like below
class Base{
public virtual string VirtualMethod(){
return "base virtual";
}
public string NotVirtual(){
return "base non virtual";
}
}
class Derived : Base{
public override string VirtualMethod(){
return "Derived overriden";
}
public new string NotVirtual(){
return "new method defined in derived";
}
}
}
if you use the code as below
Base b = new Base();
Derived d = new Derived();
Base b2 = d;
Console.WriteLine(b.VirtualMethod());
Console.WriteLine(d.VirtualMethod());
Console.WriteLine(b2.VirtualMethod());
Console.WriteLine(b.NotVirtual());
Console.WriteLine(d.NotVirtual());
Console.WriteLine(b2.NotVirtual());
It's worth paying attention to that b2 a d is the exact same object!
the result of the above would be:
base virtual
Derived overriden
Derived overriden
base non virtual
new method defined in derived
base non virtual
Eventhough the last two lines call a method name NotVirtual on the same object. Because te variables are declared of Base and Derived and the method is not virtual the declared type of the variable determine the method called whereas if the method is virtual the runtime type of the object is the determining factor for which method that will be called
Upvotes: 1
Reputation: 30488
Given a base class:
class SomeBaseClass()
{
public virtual string GetName()
{
return "SomeBaseClass";
}
}
When you override it, you inherit the function
class SomeDerivedClass() : SomeBaseClass
{
}
So when you say:
SomeDerivedClass sdc = new SomeDerivedClass();
Console.WriteLine(sdc.GetName()); //outputs "SomeBaseClass"
GetName()
returns "SomeBaseClass"
You can, however, override
it.
class SomeDerivedClass()
{
public override string GetName()
{
return "SomeDerivedClass";
}
}
Here GetName()
will now return "SomeDerivedClass"
Upvotes: 0
Reputation: 244692
You only need virtual functions if you've set up an inheritance chain, and you want to override the default implementation of a function defined in the base class in a derived class.
The classic example is something as follows:
public class Animal
{
public virtual void Speak()
{
Console.WriteLine("...");
}
}
public class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Bow-wow");
}
}
public class Human : Animal
{
public override void Speak()
{
Console.WriteLine("Hello, how are you?");
}
}
Both the Dog
and Human
classes inherit from the base Animal
class, because they're both types of animals. But they both speak in very different ways, so they need to override the Speak
function to provide their own unique implementation.
In certain circumstances, it can be beneficial to use the same pattern when designing your own classes because this enables polymorphism, which is essentially where different classes share a common interface and can be handled similarly by your code.
But I'll echo what others have suggested in the comments: learning object-oriented programming properly is not something you're going to be able to do by asking a few Stack Overflow questions. It's a complicated topic, and one well-worth investing your time as a developer learning. I highly advise picking up a book on object-oriented programming (and in particular, one written for the C# language) and going through the examples. OOP is a very powerful tool when used correctly, but can definitely become a hindrance when designed poorly!
Upvotes: 0
Reputation: 29801
Virtual functions are used if you wish to alter (override) the behavior of the function in a subclass.
class Order
{
public virtual long GetOrderNumber { return nextOrderNumber; }
}
class ExpressOrder : Order
{
public override long GetOrderNumber { return nextOrderNumber + 1000000; }
}
Upvotes: 0
Reputation: 292355
Virtual methods are the key to polymorphism. A method marked as virtual can be overriden in derived classes, to alter or specialize the behavior of the class.
Example:
class Base
{
public virtual void SayHello()
{
Console.WriteLine("Hello from Base");
}
}
class Derived : Base
{
public override void SayHello()
{
Console.WriteLine("Hello from Derived");
}
}
static void Main()
{
Base x = new Base();
x.SayHello(); // Prints "Hello from Base"
x = new Derived();
x.SayHello(); // Prints "Hello from Derived"
}
Note that you can redeclare (not override) a method that is not virtual, but in that case it won't participate in polymorphism:
class Base
{
public void SayHello() // Not virtual
{
Console.WriteLine("Hello from Base");
}
}
class Derived : Base
{
public new void SayHello() // Hides method from base class
{
Console.WriteLine("Hello from Derived");
}
}
static void Main()
{
Base x = new Base();
x.SayHello(); // Prints "Hello from Base"
x = new Derived();
x.SayHello(); // Still prints "Hello from Base" because x is declared as Base
Derived y = new Derived();
y.SayHello(); // Prints "Hello from Derived" because y is declared as Derived
}
Upvotes: 13