|
Subject:
|
when implementing two interfaces
|
|
Posted By:
|
ratheesh_param
|
Post Date:
|
11/7/2006 5:19:37 AM
|
hi all
Can anyone tell me .. when we implement a class with two interfaces which have same methods.. which reference the class will take?
public interface Interface function fun() as string end function end interface
public interface Interface1 function fun() as string end function end interface
public class : implements interface,interface1
public function classInter()As String Implements interface1.fun,interface.fun /* implementation */ end function end class
This is the case.... here the ClassInter implements the fun() from which interface? regards Ratheesh
|
|
Reply By:
|
Bob Bedell
|
Reply Date:
|
11/8/2006 10:40:08 PM
|
In both VB.NET and C# you need to implement two functions/methods in the type implementing the interfaces with the duplicate method name, one funtion/method for each version of the duplicate method:
The C# compiler supports "interface-qualified names" for methods, but the VB.NET compiler doesn't. So the VB.NET version would need to look something like this:
Public Interface InterfaceA
Function ReturnAString() As String
End Interface
Public Interface InterfaceB
Function ReturnAString() As String
End Interface
Class Class1
Implements InterfaceA
Implements InterfaceB
Public Function InterfaceAReturnAString() As String _
Implements InterfaceA.ReturnAString
Return "InterfaceA implementation"
End Function
Public Function InterfaceBReturnAString() As String _
Implements InterfaceB.ReturnAString
Return "InterfaceB implementation"
End Function
End Class
Module Module1
Sub Main()
Dim TheClass As Class1 = New Class1
Dim TheString As String
TheString = TheClass.InterfaceAReturnAString()
TheString = TheClass.InterfaceBReturnAString()
End Sub
End Module
In C# you would use interface-qualified method names in the type implementing the interfaces, then cast the type to the interface whose version of the method you want to execute. So the C# version would need to look something like this:
using System;
public interface InterfaceA {
string ReturnAString();
}
public interface InterfaceB {
string ReturnAString();
}
class Class1 : InterfaceA, InterfaceB {
// Notice no access modifier. Method considered private.
// Can't be called by a variable referencing the type
// because the interface method is implemented using
// an "ïnterface-qualified name".
string InterfaceA.ReturnAString() {
return "InterfaceA implementation";
}
string InterfaceB.ReturnAString() {
return "InterfaceB implementation";
}
static void Main() {
Class1 TheClass = new Class1();
string TheString;
// Return InterfaceA's implementation. Cast the reference
// to TheClass to an InterfaceA, and only the method defined
// by interfaceA is callable.
TheString = ((InterfaceA)TheClass).ReturnAString();
// Return InterfaceB's implementation.
TheString = ((InterfaceB)TheClass).ReturnAString();
}
}
HTH,
Bob
|
|
Reply By:
|
Bob Bedell
|
Reply Date:
|
11/8/2006 11:05:21 PM
|
In VB.NET you might want to use method names like:
Public Function InterfaceA_ReturnAString() As String _ Implements InterfaceA.ReturnAString Return "InterfaceA implementation" End Function
The underscore character helps clarify the type.member relationship in Class View, Object Browser, Intellisense, etc. by mimicing an interface-qualified name.
Bob
|
|
Reply By:
|
woodyz
|
Reply Date:
|
11/9/2006 10:48:54 AM
|
quote: Originally posted by ratheesh_param
hi all
Can anyone tell me .. when we implement a class with two interfaces which have same methods.. which reference the class will take?
public interface Interface function fun() as string end function end interface
public interface Interface1 function fun() as string end function end interface
public class : implements interface,interface1
public function classInter()As String Implements interface1.fun,interface.fun /* implementation */ end function end class
This is the case.... here the ClassInter implements the fun() from which interface? regards Ratheesh
Your classInter method is implementing both methods. If you were to call this from an object that is declared as Interface1 or Interface2 you would get the same results. If you need different results you can follow the code that Bob Bedell provided.
However, there is one aspect to his code that should be clarified. In VB.NET you are still able to call either method using its interface name (just like C#) as long as the instance is cast or declared as the type of the interface you are wanting to use, which of course is one of the main typical uses of implementing interfaces.
Dim a As New Class1 Debug.WriteLine(CType(a, InterfaceA).ReturnAString) Debug.WriteLine(CType(a, InterfaceB).ReturnAString)
Woody Z http://www.learntoprogramnow.com
|
|
Reply By:
|
Bob Bedell
|
Reply Date:
|
11/9/2006 9:58:17 PM
|
Thanks Woody Z. And to carry your point a little further, I've declared InterfaceAReturnAString and InterfaceBReturnAString with Public scope, which allows calling them through either a variable referencing the type, or a variable referencing the type cast to the interface.
Using (CType(a, InterfaceA).ReturnAString) allows the two methods to be declared with Private scope. The remaining difference then between the VB.NET version and the C# version is that C#'s interface-qualified method names automatically enforce private scope on the method declarations.
Bob
|
|
Reply By:
|
dparsons
|
Reply Date:
|
11/10/2006 12:52:37 PM
|
It would also be good to point out that you can not make Interface members static
Allowed: string ReturnAString();
Disallowed: static string ReturnAString();
And, if memory serves me, they must be declared with AT LEAST protected scope.
------------------------- I will only tell you how to do it, not do it for you. Unless, of course, you want to hire me to do work for you.
^^Thats my signature
|