Reputation: 11354
experts,
below is code for abstract class with virtual method and override in class B.
Can we call method M1 of class A, as we can't instantiate this? thanks,
public abstract class A
{
public virtual int M1(int a, int b)
{
return a + b;
}
}
public class B : A
{
public override int M1(int a, int b)
{
return a - b;
}
}
Upvotes: 0
Views: 74
Reputation: 125650
As soon as B
overrides M1
virtual method dispatch will make B.M1()
being called, even when you cast instance of B
to A
.
The only place you can call it is from within B
code, using base.M1()
syntax:
public override int M1(int a, int b)
{
var temp = base.M1(a, b);
return temp - a - b;
}
However, there can be another class inheriting from A
(lets name it C
), which does not override M1
. In that case, calling M1
on instance of C
will invoke A.M1()
.
Upvotes: 1
Reputation: 193
You must instantiate a class that implements A1, virtual is a method you CAN override but if you don't it will execute the base class method.
Upvotes: 0