Reputation: 11
I have an Interface
interface ISampleInterface
{
void SampleMethod();
}
and a class which is inherited from interface
public class ImplementationClass : ISampleInterface
{
// Explicit interface member implementation:
public virtual void SampleMethod()
{
Console.WriteLine("Base");
}
}
public class derived : ImplementationClass
{
public override void SampleMethod()
{
Console.WriteLine("child");
}
}
Now i want to call SampleMethod
from derived class if user creates derived class if not then call from ImplementationClass
which is base class,
basically i have to decide at run time weather derived class exists with the SampleMethod
implementation if yes go and call else go to base SampleMethod
.
Upvotes: 0
Views: 315
Reputation: 778
i want to call SampleMethod from Derived class but derived class is created by some other modules, i have to check weather the derived class exists Or not if it exists call derived method else call base mathod
Per the comments on your question, this code is already working as you've described - because of polymorphism, the created object will call it's most relevant method.
In your case you override the virtual method of it's base class so that's the method that will be called.
Upvotes: 1