interface question

J

John Underwood

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
 
B

Bob Grommes

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
 
C

Christopher Kimbell

Interfaces provide another level of abstraction, you can have two objects
that implement the same interface, but are implemented completely different.
The System.Data namespace contains a number of interfaces like IDataReader
and IDataAdapter. If your code uses the interfaces instead of the actual
class, it would be easier to swap between Sql and Oracle data providers.

Interfaces can also reduce the number of assemblies that has to be rebuild
when something changes. If you change an assembly that has a strong name,
any assembly that uses the changed assembly has to be rebuilt. You can get
around this with some configuration, but it's not very nice. If you seperate
the interfaces into an assembly, the users will reference this assembly, not
the implementing one. If the interface hasn't changed, you only have to
rebuild the implementing assembly. This is very useful in large complex
systems.

Interfaces are very usefull when you want to build extensible software, like
providing a plugin.


I hope this shows you the value of interfaces.

Chris
 
N

Nurchi BECHED

Hello, John!

In addition to everything said, here is a "life" example...
Let's say you create a class called 'Cat'
Where you definitely 'describe' how it should catch mice.
Later on you create a new class called 'Snake'
Which can also catch mice, but not the same way as the 'Cat'...
Now you build your application that uses 'Cat' and 'Snake'
somewhere and somehow...
What happens if someone "creates" a 'MouseTrap' class
that catches mice completely different way than 'Cat' and 'Snake'
or inventive Americans (no offence) teach penguins to catch
mice...
Will you rewrite your whole application each and every time there
is a new "MouseCatcher"?...

Now here is a point...
All you have to do is just create an interface called 'MouseCatcher'
and use it in your application just once.
It doesn't matter HOW your Cat or MouseTrap or Snake or something
else catches mice... all that matters is that they all DO CATCH MICE!

So, in your interface you declare (only declare!) a method called
(something like) CatchAMouse() (maybe with some parameters...)
And then you just make calls to that method in your application.
Your (actually your application's) job is to tell the 'MouseCatcher' to
catch a mouse, no matter how...
That's it.

In the example you provided, you can create another class like 'MyPoint2'
and tell it to do something when you assign or get values for X and Y...
Let's do something stupid:
-----------------------------------------------------
class MyPoint2 : 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;
MessageBox.Show("A value for 'X' has been returned!!!");
}

set
{
myX = value;
MessageBox.Show("A value for 'X' has been set!!!");
}
}

public int y
{
get
{
return myY;
MessageBox.Show("A value for 'Y' has been returned!!!");
}
set
{
myY = value;
MessageBox.Show("A value for 'Y' has been set!!!");
}
}
}
-----------------------------------------------------
Now use this code in your example instear of MyPoint
and see what happens.

Get it?
Hope this helps.


You wrote on 8 May 2004 12:44:39 -0700:

JU> Thanks,

JU> John Underwood

JU> Visual Studio .Net example below:
JU> Example
JU> The following example demonstrates interface implementation. In this
JU> example, the interface IPoint contains the property declaration, which
JU> is responsible for setting and getting the values of the fields. The
JU> class MyPoint contains the property implementation.

JU> // keyword_interface.cs
JU> // Interface implementation
JU> using System;
JU> interface IPoint
JU> {
JU> // Property signatures:
JU> int x
JU> {
JU> get;
JU> set;
JU> }

JU> int y
JU> {
JU> get;
JU> set;
JU> }
JU> }

JU> class MyPoint : IPoint
JU> {
JU> // Fields:
JU> private int myX;
JU> private int myY;

JU> // Constructor:
JU> public MyPoint(int x, int y)
JU> {
JU> myX = x;
JU> myY = y;
JU> }

JU> // Property implementation:
JU> public int x
JU> {
JU> get
JU> {
JU> return myX;
JU> }

JU> set
JU> {
JU> myX = value;
JU> }
JU> }

JU> public int y
JU> {
JU> get
JU> {
JU> return myY;
JU> }
JU> set
JU> {
JU> myY = value;
JU> }
JU> }
JU> }

JU> class MainClass
JU> {
JU> private static void PrintPoint(IPoint p)
JU> {
JU> Console.WriteLine("x={0}, y={1}", p.x, p.y);
JU> }


With best regards, Nurchi BECHED.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top