Monday, March 28, 2011

Implementing two interfaces which define same function

Lets say we have two interfaces named "Intr1", "Iintr2" and both the interfaces contains a function named "myFunction()".

now lets say, we have a class named "myClass" and we are implementing both the interfaces in that class. so now because of rule of implementation, we will have to override/define all the function of interface "Intr1" and "Intr2" in class.

now the twist comes to story. as we have a function with same name "myFunction()" in both the interfaces, how will we provide functionality to it in class.

below is an code snippet of how to provide functionality to function in such situation:
interface Intr1
{
    void myFunction();
}

interface Intr2
{
    void myFunciton();
}

class myClass: Intr1,Intr2 
{
    public void Intr1.myFunction()
    {
   // code here functionality for "myFunciton" of interface "Intr1"
    }

    public void Intr2.myFunction()
    {
 // code here functionality for "myFunciton" of interface "Intr2"
    }
}



Solution By:Rajesh Rolen

Share This!


2 comments:

Anonymous said...

Its good solution...

chhatrapati said...

but i think it's use less because we can those common method through implemented class object

Powered By Blogger · Designed By Seo Blogger Templates