Interface question

  • Thread starter Thread starter george r smith
  • Start date Start date
G

george r smith

I thought that you it is forbidden to create an instance of an interface.

But a help example (search for Explicit Interface Implementation Sample)
from visual studio has the following sample code:

// Declare an instance of the English units interface:
IEnglishDimensions eDimensions = (IEnglishDimensions) myBox;
// Declare an instance of the metric units interface:
IMetricDimensions mDimensions = (IMetricDimensions) myBox;

So is this a mistatement - will someone put into words what the above
statements are doing.
thanks
grs
 
These statements do not create instances of the
interfaces. What they do is cast to the interfaces. So,
myBox instance probably implements both IEnglishDimensions
and IMetricDimensions in its class definition.

Tu-Thach
 
That is what I thought ("cast to the interface") but when you see an example
in the IDE help files it makes you ponder and uncertain.
 
Hi George,

Thanks for posting in this group.
The implement of interface must be placed in the inherited class. And the
implementation of interface has 2 types: explicit and implicit.
When explicit implement the interface method, you should specify the
interface name with a dot before the method name.
This can be used to distinguish 2 different interface's methods
implementation.(May have the same name)
And the explicit implemented interface method can not be directly accessed
from the class instance, you must convert the class instance to certain
instance and then invoke this method.
For your code snippet, I think your myBox implemented both
IEnglishDimensions and IMetricDimensions, then if you want to use the
methods of IEnglishDimensions, you'd better cast myBox into
IEnglishDimensions and invoke its methods.(Whether the cast is a must
depands on whether you implement IEnglishDimensions explicit or implicit).
The same is for IMetricDimensions.
These 2 behavior did not create an instance of any interface, they just get
the instance of the interface(which was contained in class instance), and
use it. So there is no mistatement.

For more information of "Explicit Interface Implementation", you can refer
to the tutorial below:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/
vcwlkexplicitinterfaceimplementationtutorial.asp

If you still have anything unclear, please feel free to tell me.
Have nice day!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
George... Except for strings or factory methods, you use the key word
new to create an instance of an class. eDimensions is a variable that
contains a reference of type IEnglishDimensions that is totally separate
from the actual object of class MyBox. So references have type and
objects have class. More here:

http://www.geocities.com/jeff_louie/OOP/oop6.htm

Regards,
Jeff
I thought that you it is forbidden to create an instance of an
interface.<
 
Jeff,
WHAT A GREAT "mantra", "Objects have class, references have type."
Thanks for the reply and thanks for the statement - sure helps to have a
catchy phrase
to aid the memory.
grs
 
Back
Top