interface S1
{
public void ShowMessage();
}
interface S2
{
public void ShowMessage();
}
public class Program : S1, S2
{
public void ShowMessage()
{
Console.WriteLine("Show Message..............");
}
static void Main(string[] args)
{
Program p = new Program();
p.ShowMessage();
}
}
What is the output ?
if there is an error or if it is not shown, why
Can we use explicit interface here

Jignesh KumarPosted Mar 2, 2025, 11:16 AM
Out put is :
Show Message..............
Is there any error ?
No, there is no compilation or runtime error.
Since both interfaces declare the same method signature, the class
Programprovides a single implementation, which is valid.Can we use explicit interface here?
Yes, we can use explicit interface implementation if we want to provide separate implementations for each interface.
Prasad RaveendranPosted Mar 3, 2025, 1:24 AM
S1 s1 = p;).S1.ShowMessage()andS2.ShowMessage().Programinstance (p.ShowMessage();would cause an error).Emily FosterPosted Mar 2, 2025, 5:04 AM
In the provided code snippet, the Program class implements two interfaces, S1 and S2, both of which declare the same method signature, `ShowMessage()`. The `ShowMessage()` method in the Program class contains the implementation to print "Show Message.............." to the console.
When the Main method is executed, an instance of the Program class is created, and then the `ShowMessage()` method is called. In this case, the output will be:
There won't be any errors because the Program class fulfills the contract of both interfaces by providing an implementation for the `ShowMessage()` method defined in each interface.
Regarding the use of explicit interfaces here, you are not required to use explicit interface implementations in this case since the method `ShowMessage()` is the same in both interfaces `S1` and `S2`. Explicit interface implementations are typically used when the same method signature exists in multiple interfaces but must be implemented differently. Since the method implementation is the same in this scenario, you can directly implement the method in the class without the need for explicit interface implementation.
If you have any further questions or need more clarification, feel free to ask!