|
-
Oct 27th, 2006, 11:40 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Interface Usage. Am I doing this right?
book says that interfaces specify behavior without implementing it. That doesn't really explain much to me.
If you could please look at what I've done and tell me if I am using the interface properly, I would really appreciate it. What I've done seems to work. But I don't understand why I would want to use an interface. It seems to me that it's some way to pass objects around and use methods easier? But I am not sure since it seems I am repeating methods anyways.
If you think it could be done in a better way, I would love to know how. But my main hope is that I am doing this correctly and could use some help with understanding how to use an interface.
Thanks so much in advance.
VB Code:
class Arm : IBendable
{
public Arm()
{
}
public void Raise()
{
Console.WriteLine("Raising arm up.");
}
public void Bend()
{
this.Raise();
}
}
VB Code:
class Spoon : IBendable
{
public Spoon()
{
}
public void Eat()
{
Console.WriteLine("Eating from the spoon.");
}
public void Bend()
{
this.Eat();
}
}
VB Code:
namespace Prog6_18
{
public interface IBendable
{
// Any class that implements the IShape interface
// must define the following methods:
void Bend();
}
}
VB Code:
class Prog6_18
{
static void Main(string[] args)
{
Spoon childsSpoon = new Spoon();
Arm childsArm = new Arm();
EatFood(childsArm);
EatFood(childsSpoon);
}
public static void EatFood(IBendable a)
{
a.Bend();
}
}
-
Oct 28th, 2006, 03:26 AM
#2
Re: Interface Usage. Am I doing this right?
As you correctly state/quote, interfaces specify behaviour without implementing it.
What does this mean? It means that you can be assured that a class that implements an interface will possess the functionality declared in the interface.
How does this differ from inheritance? Well, inheritance is about basing classes upon other classes, so you start with the functionality already present in the base class.
Basically, inheritance specifies what something is, while interfaces specify what something does.
For example, you could inherit from a "Cutlery" class, because that's what a spoon is:
Code:
abstract class Cutlery
{
// Some code that is common to items of cutlery.
};
class Spoon : Cutlery {};
You don't have to do anything more at this point: you already have an item of cutlery.
Why is the Cutlery class abstract? Because cutlery is an abstract concept: you cannot have just an item of cutlery, it must be a spoon, or a fork, etc.
Now, suppose for the sake of example that we have bendable spoons and non-bendable forks. We need an interface to specify which ones are bendable:
Code:
interface Bendable
{
public void Bend();
}
Any item of cutlery may implement the Bendable interface.
In implementing an interface, you must implement each of its methods:
Code:
class Spoon : Cutlery, Bendable
{
public void Bend()
{
// some code to bend the spoon
}
}
class Fork : Cutlery
{
// no Bend() method.
}
All very well. But how is this at all useful?
Well, suppose we want to write some code that takes something bendable and does something to it - presumably bending it.
Code:
void BendSomething(Bendable thing)
{
thing.Bend();
}
BendSomething() now will only accept objects that implement the Bendable interface. The responsibility of making the code work is transferred to the person who calls the BendSomething() method; they must ensure that the object they pass to it implements the Bendable interface. This is key to the principle of writing reusable, abstract code.
So we can bend a spoon:
Code:
Spoon my_spoon = new Spoon();
BendSomething(my_spoon); // All goes well.
If we try to bend a fork, on the other hand, the compiler will call it out:
Code:
Fork my_fork = new Fork();
BendSomething(my_fork); // Build error as Fork does not implement Bendable.
I hope that goes some way toward explaining these concepts for you. Obviously, it's a pretty contrived example, but it should illustrate the principle.
-
Oct 28th, 2006, 06:10 AM
#3
Re: Interface Usage. Am I doing this right?
If you want to see class inheritance and interface implementation in action for real I suggest that you take a look at the DbDataAdapter class and IDbDataAdapter interface. Classes like OleDbDataAdapter, SqlDataAdapter, OdbcDataAdapter and all other data adapter classes inherit DbDataAdapter and implement IDbDataAdapter. There are also the DbConnection/IDbConnection and DbCommand/IDbCommand pairs as well.
-
Oct 28th, 2006, 09:54 AM
#4
Thread Starter
Hyperactive Member
Re: Interface Usage. Am I doing this right?
Thank you penagate. That was very clear and understandable for me. It helps so much when I can understand why we would want to use something and when.
Kudos!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|