Re: interface question

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Bob Grommes (bob_at_bobgrommes.com)
Date: 05/08/04


Date: Sat, 8 May 2004 13:31:38 -0700

John,

In my view the IPoint example is a trivial example that demonstrates the
syntax without providing any insight into its usefulness.

An interface is useful IMO mainly when you have a bit of functionality that
needs to be defined in more than one class, and not all of those classes
share the same inheritance tree. If you look at where MSFT used interfaces
within the CLR, you'll see many examples of this. For example, the
IDisposable interface defines standard functionality for releasing unmanaged
resources such as database connections or file handles. That capability
might be needed across a wide variety of classes, and an interface
definition allows it to be implemented in a uniform fashion even though the
main purpose of any two classes that inherit from IDisposable might be
completely different and unrelated.

Interfaces work best when the actual implementation is likely to be very
specific to the class that inherits the interface (again, IDisposable is a
great example of this; the implementation must know a great deal about the
internals and specifics of the class it's in, but a client of IDisposable is
completely spared these details).

Another useful aspect of an interface is that you may sometimes need to
manipulate only the portion of a class that implements some interface. In
that situation you can cast to the interface rather than to the class type,
and then you have access only to the interface members. In other words you
have visibility only to the members that make sense in the current context,
plus you can access that functionality from a known, single type and you
needn't know anything else about the rest of the class and what it does.

In practice I don't create interfaces in application-level code that often,
but when I do, they are quite useful.

--Bob

"John Underwood" <johnwadeunderwood@yahoo.com> wrote in message
news:f004e9c2.0405081144.6fd27f6@posting.google.com...
> Hi.. I was looking at interface, and I have a example in the docs i'll
> paste below.. I'm not grasping what you would gain by using a
> interface, does any one have a brief description of their benefit?
>
>
> Thanks,
>
> John Underwood
>
>
> Visual Studio .Net example below:
> Example
> The following example demonstrates interface implementation. In this
> example, the interface IPoint contains the property declaration, which
> is responsible for setting and getting the values of the fields. The
> class MyPoint contains the property implementation.
>
> // keyword_interface.cs
> // Interface implementation
> using System;
> interface IPoint
> {
> // Property signatures:
> int x
> {
> get;
> set;
> }
>
> int y
> {
> get;
> set;
> }
> }
>
> class MyPoint : IPoint
> {
> // Fields:
> private int myX;
> private int myY;
>
> // Constructor:
> public MyPoint(int x, int y)
> {
> myX = x;
> myY = y;
> }
>
> // Property implementation:
> public int x
> {
> get
> {
> return myX;
> }
>
> set
> {
> myX = value;
> }
> }
>
> public int y
> {
> get
> {
> return myY;
> }
> set
> {
> myY = value;
> }
> }
> }
>
> class MainClass
> {
> private static void PrintPoint(IPoint p)
> {
> Console.WriteLine("x={0}, y={1}", p.x, p.y);
> }
>
> public static void Main()
> {
> MyPoint p = new MyPoint(2,3);
> Console.Write("My Point: ");
> PrintPoint(p);
> }
> }
> Output
> My Point: x=2, y=3



Relevant Pages

  • RFC: if_clone overhaul
    ... Please test/review the following patch to the network interface cloneing ... static void ... -if_clone_lookup(const char *name, int *unitp) ... static int gifmodevent; ...
    (freebsd-net)
  • [PATCH 3/7] adding xc5000 tuner chip driver
    ... this code also uses the tunerchip.h interface which allows to attach ... It uses the latest Xceive reference driver which fixes some issues. ... new file mode 100644 ... unsigned int frequency; ...
    (Linux-Kernel)
  • [PATCH] usbmon: add binary interface
    ... USB records are stored in a liked list, alike current text interface implementation, so most code is shared from binary and text interface. ... unsigned int cmd, unsigned long arg) ... struct urb *urb, char ev_type) ...
    (Linux-Kernel)
  • Re: Possible to pass strings back to ASP from ActriveX?
    ... afx_msg int AboutBox; ... helpstring("AxTemplateTest1 ActiveX Control module"), ... // Primary dispatch interface for CAxTemplateTest1Ctrl ... helpstring("Dispatch interface for AxTemplateTest1 Control")] ...
    (microsoft.public.windowsce.embedded)
  • Re: identity...... Was: The wisdom of the object mentors
    ... // this is it's implementation/mapping from a hidden domain of {int x int} ... construction mechanism.....for this I need to show a construction! ... I'd say that the set of all propositions is language defined behavior or ... But what about implementation and the interface of a type? ...
    (comp.object)