On MSDN I have found that it is a error to use "virtual" modifier in an abstract method declaration. One of my colleagues, who should be pretty experienced developer, though uses this in his code:
public abstract class BusinessObject
{
public virtual void Render(){}
public virtual void Update(){}
}
Also it correct or not?
It can make sense if the abstract class is providing an optional point where inherited classes can change the behaviour.
So this way the inherited classes will not be forced to implement it but they can if they need to.
Usually this method is called by the abstract class:
public AddFoo(Foo f)
{
// ...
OnAddedFoo(f);
}
Here it makes sense to have OnAddedFoo
as virtual
and not abstract
.
I guess the MSDN means you can't use the virtual
and abstract
modifier on a method at the same time.
You can either do
public virtual void Render() {}
which means that an inheriting class can override this method.
Or you can do
public abstract void Render();
which means that an inheriting class must override this method.
Those aren't abstract methods, they're empty default ones. The difference is you don't HAVE to override them (if you don't, nothing will happen).
It might help your understanding to see them formatted like:
public abstract class BusinessObject
{
public virtual void Render()
{
}
public virtual void Update()
{
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With