Reputation: 1979
I'm not exactly sure how to ask this question, but basically what I want to do is access the abstract methods from IDog. Consider this:
static void Main(string[] args)
{
IDog dog = new Dog();
dog.CatchFrisbee();
dog.Speak("Bark") // error -> no implementation
Console.ReadLine();
}
public class Dog : Animal, IDog
{
public void CatchFrisbee()
{
Console.WriteLine("Dog Catching Frisbee");
}
}
public abstract class Animal : IAnimal
{
public void Speak(string sayWhat)
{
Console.WriteLine(sayWhat);
}
public void Die()
{
Console.WriteLine("No Longer Exists");
}
}
public interface IDog
{
void CatchFrisbee();
}
public interface IAnimal
{
void Die();
void Speak(string sayWhat);
}
From my static void Main, I would like to be able to call dog.Speak() but I can't because it's not implemented in IDog. I know I can easily access Speak() from my derived classes but is it possible to access it from the implemenation or would that be a bad design?
Upvotes: 1
Views: 88
Reputation: 70718
You could perform a cast:
((Animal) dog).Speak("Bark");
Or take advantage of multiple interface inheritance:
public interface IDog : IAnimal
{
void CatchFrisbee();
}
Upvotes: 1
Reputation: 120380
Assuming that all IDog
s are IAnimal
s, declare IDog
as implementing IAnimal
public interface IDog : IAnimal
Upvotes: 6