Interviews3 answers
How can we use the same method of different interface?
3 Answers
By explicit type casting of method
Mukesh KumarPosted Sep 4, 2017, 11:20 PM EDT
interface IAnimal{ string Walk();}
interface Ihuman{ string Walk();}
public class Iimplement: IAnimal,Ihuman
{
public string IAnimal.Walk(){return "Snaks crawling..."}
public string Ihuman.Walk(){return "men walking..."}
}
Mohsin MukriPosted Jun 27, 2017, 2:01 AM EDT
public interface ITalk
{
string Shout();
}
public class Dog : ITalk
{
public string Shout()
{
return "Woof, woof woof woof woof";
}
}
public class Cat : ITalk
{
public string Shout()
{
return "Mew Mew Mew";
}
}
public class Parrot : ITalk
{
public string Shout()
{
return "Sqwark! Sqwark! Sqwark!";
}
}
public class Program
{
static void Main(string[] args)
{
// Writes Woof, Woof
ITalk pet = new Dog();
Console.WriteLine(pet .Speak());
// Now writes Meow
ITalk pet = new Cat();
Console.WriteLine(pet .Speak());
// Now writes Sqwark etc
ITalk pet = new Parrot();
Console.WriteLine(pet .Speak());
}
}
AMIT KUMARPosted Sep 9, 2016, 5:20 AM EDT
