Interfaces

  • Thread starter Michael Grimsley
  • Start date
M

Michael Grimsley

I have been looking at interfaces in C# and cannot seem to understand them. I am told that interfaces resembles a class with no code and that methods declared in an interface are not allowed to contain method bodies. My question is what purpose does creating an interface serve that has methods with not bodies? If a method does not have a body then what is the methods purpose, because if you call the method it will not do anything or you have to create another method in your class that does the work. As an example:

interface IToken
{
string Name();
}

class Token: IToken
{
public string Name ()
{
some code;
}
}

What good does it due to have a method with no body and then create the method in your class? From the examples I have seen it looks like this is extra work, but yet it is recommended that a programmer program to interfaces. This makes no sense to me and your help is appreciated.
 
M

Morten Wennevik

Hi Michael,

I answered this very question about a week ago (well involving abstract classes), so I'll just copy and paste the answer here. There is more to interfaces than my answer tells you, but it might give you an idea what they can be used for.

The following is taken from an earlier post
-------------------------------------------
Dear Friends ,
Please tell me following thing -
Que -: what is the main use of interface in .net ?

Interfaces are used to pass objects that are completely different but who all have some methods that are defined in the interface.

Consider a class Rock and a class House. Completely different, and nothing in common, almost. For instance, they both can drop down and crash, say in case of an earthquake :p. Now, you have a class
Earthquake with a SmashObjects method. Instead of having to create a parent class for everything on earth that can drop, and have every class, like Rock and House inherit that class, you can make an
interface IDroppable and have Rock and House implement that interface. In the interface you define Drop(); and now SmashObject can smash every object that implements IDroppable because it knows, no
matter how different, they all have a Drop method.

interface IDroppable
{
Drop();
}

class House : IDroppable
{
Drop()
{
// do house dropping stuff
}
}

class Rock : Lava, IDroppable
{
Drop()
{
// do rock dropping stuff
}
}

class Earthquake
{
SmashObjects(IDroppable thingy)
{
thingy.Drop();
}
}

Now, in this sample you could have accomplished the same by making a base parent class and overriding the Drop method, but what if the earthquake wants to roll something. Now, the rock can roll, but
not the house (not really), but a car can roll. So, make a new interface IRollable and have the rock and car implement that interface. You can only inherit a single class, but implement numerous
interfaces. So basically, what I am trying to safe, interfaces describe some common abilities. Oh, and the I in front of interfaces is intentional to distinguish them from inherited classes.
Que -: What is difference between abstract class and interface?

An abstract class is a blueprint for a new class, it will act in a similar way as a single interface, but as mentioned above, you can only inherit one class, and I find interfaces to be cleaner and
easier to maintain.
Que -: How to make class in Object Oriented Form? (in c#)

Eh? Like this?

class MyForm : System.Windows.Forms.Form
{
MyForm()
{
}
}



Happy coding!
Morten Wennevik [C# MVP]
 
M

Michael Grimsley

Morten;

Thanks for getting back to me. I still do not follow what is going on. You
are still defining the method twice, once in the interface that does nothing
and once in your class that does the work. So when you want to use the
method, which method is called, the one in the interface that no work or the
method where you defined it to do some work?

Thanks

Michael


Morten Wennevik said:
Hi Michael,

I answered this very question about a week ago (well involving abstract
classes), so I'll just copy and paste the answer here. There is more to
interfaces than my answer tells you, but it might give you an idea what they
can be used for.
The following is taken from an earlier post
all have some methods that are defined in the interface.
Consider a class Rock and a class House. Completely different, and
nothing in common, almost. For instance, they both can drop down and crash,
say in case of an earthquake :p. Now, you have a class
Earthquake with a SmashObjects method. Instead of having to create a
parent class for everything on earth that can drop, and have every class,
like Rock and House inherit that class, you can make an
interface IDroppable and have Rock and House implement that interface. In
the interface you define Drop(); and now SmashObject can smash every object
that implements IDroppable because it knows, no
matter how different, they all have a Drop method.

interface IDroppable
{
Drop();
}

class House : IDroppable
{
Drop()
{
// do house dropping stuff
}
}

class Rock : Lava, IDroppable
{
Drop()
{
// do rock dropping stuff
}
}

class Earthquake
{
SmashObjects(IDroppable thingy)
{
thingy.Drop();
}
}

Now, in this sample you could have accomplished the same by making a base
parent class and overriding the Drop method, but what if the earthquake
wants to roll something. Now, the rock can roll, but
not the house (not really), but a car can roll. So, make a new interface
IRollable and have the rock and car implement that interface. You can only
inherit a single class, but implement numerous
interfaces. So basically, what I am trying to safe, interfaces describe
some common abilities. Oh, and the I in front of interfaces is intentional
to distinguish them from inherited classes.
An abstract class is a blueprint for a new class, it will act in a similar
way as a single interface, but as mentioned above, you can only inherit one
class, and I find interfaces to be cleaner and
easier to maintain.
Que -: How to make class in Object Oriented Form? (in c#)

Eh? Like this?

class MyForm : System.Windows.Forms.Form
{
MyForm()
{
}
}



Happy coding!
Morten Wennevik [C# MVP]
 
C

Cliff Harris

When you define an interface, the methods within are never actually called.
An Interface just says, "Whoever inherits from me if guarenteed to have all
methods listed within me implemented."
When I implement an interface, all I am doing is telling everyone that I do,
in fact, contain the methods that are listed in the interface.
That way, my class, no matter what it does, can be cast into any Interface
that it implements without whatever is using my class knowing anything about
it.

This article:
http://msdn.microsoft.com/library/d...ry/en-us/csref/html/vcreftheinterfacetype.asp
Describes an interface as a "contract" and a class that implements an
interface is saying that it ahere's to the contract.... maybe the article
will explain it better...

I hope this was helpfull rather than more confusing...

-Cliff

Michael Grimsley said:
Morten;

Thanks for getting back to me. I still do not follow what is going on. You
are still defining the method twice, once in the interface that does nothing
and once in your class that does the work. So when you want to use the
method, which method is called, the one in the interface that no work or the
method where you defined it to do some work?

Thanks

Michael


Morten Wennevik said:
Hi Michael,

I answered this very question about a week ago (well involving abstract
classes), so I'll just copy and paste the answer here. There is more to
interfaces than my answer tells you, but it might give you an idea what they
can be used for.
The following is taken from an earlier post
who
all have some methods that are defined in the interface.
Consider a class Rock and a class House. Completely different, and
nothing in common, almost. For instance, they both can drop down and crash,
say in case of an earthquake :p. Now, you have a class
Earthquake with a SmashObjects method. Instead of having to create a
parent class for everything on earth that can drop, and have every class,
like Rock and House inherit that class, you can make an
interface IDroppable and have Rock and House implement that interface.
In
the interface you define Drop(); and now SmashObject can smash every object
that implements IDroppable because it knows, no
matter how different, they all have a Drop method.

interface IDroppable
{
Drop();
}

class House : IDroppable
{
Drop()
{
// do house dropping stuff
}
}

class Rock : Lava, IDroppable
{
Drop()
{
// do rock dropping stuff
}
}

class Earthquake
{
SmashObjects(IDroppable thingy)
{
thingy.Drop();
}
}

Now, in this sample you could have accomplished the same by making a
base
parent class and overriding the Drop method, but what if the earthquake
wants to roll something. Now, the rock can roll, but
not the house (not really), but a car can roll. So, make a new
interface
IRollable and have the rock and car implement that interface. You can only
inherit a single class, but implement numerous
interfaces. So basically, what I am trying to safe, interfaces describe
some common abilities. Oh, and the I in front of interfaces is intentional
to distinguish them from inherited classes.
An abstract class is a blueprint for a new class, it will act in a
similar
way as a single interface, but as mentioned above, you can only inherit one
class, and I find interfaces to be cleaner and
easier to maintain.
Que -: How to make class in Object Oriented Form? (in c#)

Eh? Like this?

class MyForm : System.Windows.Forms.Form
{
MyForm()
{
}
}



Happy coding!
Morten Wennevik [C# MVP]
 
J

Jose Luis Manners

You can instantiate a concrete class that supports an interface and assign
the object to an interface variable. Something like this:

IMyInterface obj1;
obj1 = new MyClass();
obj1.Method1(); // obj1 could be any object as long as
implements IMyInterface


Also, if you are creating a client and server apps you can create an
assembly with interfaces to allow concurrent development on both sides.

Another benefit is that you can inherit from multiple interfaces but this is
not necessarily the same with abstract classes.

I hope this helps.

Jose Luis Manners, MCP

I have been looking at interfaces in C# and cannot seem to understand them.
I am told that interfaces resembles a class with no code and that methods
declared in an interface are not allowed to contain method bodies. My
question is what purpose does creating an interface serve that has methods
with not bodies? If a method does not have a body then what is the methods
purpose, because if you call the method it will not do anything or you have
to create another method in your class that does the work. As an example:

interface IToken
{
string Name();
}

class Token: IToken
{
public string Name ()
{
some code;
}
}

What good does it due to have a method with no body and then create the
method in your class? From the examples I have seen it looks like this is
extra work, but yet it is recommended that a programmer program to
interfaces. This makes no sense to me and your help is appreciated.
 
M

Michael Grimsley

Cliff;

Thanks that clears quite a bit up. One last question, then why use an
Interface at all-if all it is saying is that you have to have these methods,
properties, etc., in the class.

Michael


Cliff Harris said:
When you define an interface, the methods within are never actually called.
An Interface just says, "Whoever inherits from me if guarenteed to have all
methods listed within me implemented."
When I implement an interface, all I am doing is telling everyone that I do,
in fact, contain the methods that are listed in the interface.
That way, my class, no matter what it does, can be cast into any Interface
that it implements without whatever is using my class knowing anything about
it.

This article:
http://msdn.microsoft.com/library/d...ry/en-us/csref/html/vcreftheinterfacetype.asp
Describes an interface as a "contract" and a class that implements an
interface is saying that it ahere's to the contract.... maybe the article
will explain it better...

I hope this was helpfull rather than more confusing...

-Cliff

Michael Grimsley said:
Morten;

Thanks for getting back to me. I still do not follow what is going on. You
are still defining the method twice, once in the interface that does nothing
and once in your class that does the work. So when you want to use the
method, which method is called, the one in the interface that no work or the
method where you defined it to do some work?

Thanks

Michael


Morten Wennevik said:
Hi Michael,

I answered this very question about a week ago (well involving
abstract
classes), so I'll just copy and paste the answer here. There is more to
interfaces than my answer tells you, but it might give you an idea what they
can be used for.
The following is taken from an earlier post
-------------------------------------------

Dear Friends ,
Please tell me following thing -
Que -: what is the main use of interface in .net ?

Interfaces are used to pass objects that are completely different but
who
all have some methods that are defined in the interface.
Consider a class Rock and a class House. Completely different, and
nothing in common, almost. For instance, they both can drop down and crash,
say in case of an earthquake :p. Now, you have a class
Earthquake with a SmashObjects method. Instead of having to create a
parent class for everything on earth that can drop, and have every class,
like Rock and House inherit that class, you can make an
interface IDroppable and have Rock and House implement that interface.
In
the interface you define Drop(); and now SmashObject can smash every object
that implements IDroppable because it knows, no
matter how different, they all have a Drop method.

interface IDroppable
{
Drop();
}

class House : IDroppable
{
Drop()
{
// do house dropping stuff
}
}

class Rock : Lava, IDroppable
{
Drop()
{
// do rock dropping stuff
}
}

class Earthquake
{
SmashObjects(IDroppable thingy)
{
thingy.Drop();
}
}

Now, in this sample you could have accomplished the same by making a
base
parent class and overriding the Drop method, but what if the earthquake
wants to roll something. Now, the rock can roll, but
not the house (not really), but a car can roll. So, make a new
interface
IRollable and have the rock and car implement that interface. You can only
inherit a single class, but implement numerous
interfaces. So basically, what I am trying to safe, interfaces
describe
some common abilities. Oh, and the I in front of interfaces is intentional
to distinguish them from inherited classes.
Que -: What is difference between abstract class and interface?

An abstract class is a blueprint for a new class, it will act in a
similar
way as a single interface, but as mentioned above, you can only inherit one
class, and I find interfaces to be cleaner and
easier to maintain.

Que -: How to make class in Object Oriented Form? (in c#)

Eh? Like this?

class MyForm : System.Windows.Forms.Form
{
MyForm()
{
}
}



Happy coding!
Morten Wennevik [C# MVP]
 
B

Bonj of Beeeeeves

Purely so you have something common to cast object of
different types to, in order to call common methods,
when you don't know at compile time what the object is
you're calling a method on.

Michael Grimsley said:
Cliff;

Thanks that clears quite a bit up. One last question, then why use an
Interface at all-if all it is saying is that you have to have these methods,
properties, etc., in the class.

Michael


Cliff Harris said:
When you define an interface, the methods within are never actually called.
An Interface just says, "Whoever inherits from me if guarenteed to have all
methods listed within me implemented."
When I implement an interface, all I am doing is telling everyone that I do,
in fact, contain the methods that are listed in the interface.
That way, my class, no matter what it does, can be cast into any Interface
that it implements without whatever is using my class knowing anything about
it.

This article:
http://msdn.microsoft.com/library/d...ry/en-us/csref/html/vcreftheinterfacetype.asp
Describes an interface as a "contract" and a class that implements an
interface is saying that it ahere's to the contract.... maybe the article
will explain it better...

I hope this was helpfull rather than more confusing...

-Cliff

Michael Grimsley said:
Morten;

Thanks for getting back to me. I still do not follow what is going on. You
are still defining the method twice, once in the interface that does nothing
and once in your class that does the work. So when you want to use the
method, which method is called, the one in the interface that no work
or
the
method where you defined it to do some work?

Thanks

Michael


Hi Michael,

I answered this very question about a week ago (well involving abstract
classes), so I'll just copy and paste the answer here. There is more to
interfaces than my answer tells you, but it might give you an idea
what
they
can be used for.

The following is taken from an earlier post
-------------------------------------------

Dear Friends ,
Please tell me following thing -
Que -: what is the main use of interface in .net ?

Interfaces are used to pass objects that are completely different
but
who
all have some methods that are defined in the interface.

Consider a class Rock and a class House. Completely different, and
nothing in common, almost. For instance, they both can drop down and crash,
say in case of an earthquake :p. Now, you have a class
Earthquake with a SmashObjects method. Instead of having to create a
parent class for everything on earth that can drop, and have every class,
like Rock and House inherit that class, you can make an
interface IDroppable and have Rock and House implement that
interface.
In
the interface you define Drop(); and now SmashObject can smash every object
that implements IDroppable because it knows, no
matter how different, they all have a Drop method.

interface IDroppable
{
Drop();
}

class House : IDroppable
{
Drop()
{
// do house dropping stuff
}
}

class Rock : Lava, IDroppable
{
Drop()
{
// do rock dropping stuff
}
}

class Earthquake
{
SmashObjects(IDroppable thingy)
{
thingy.Drop();
}
}

Now, in this sample you could have accomplished the same by making a base
parent class and overriding the Drop method, but what if the earthquake
wants to roll something. Now, the rock can roll, but
not the house (not really), but a car can roll. So, make a new interface
IRollable and have the rock and car implement that interface. You can only
inherit a single class, but implement numerous
interfaces. So basically, what I am trying to safe, interfaces describe
some common abilities. Oh, and the I in front of interfaces is intentional
to distinguish them from inherited classes.

Que -: What is difference between abstract class and interface?

An abstract class is a blueprint for a new class, it will act in a similar
way as a single interface, but as mentioned above, you can only
inherit
one
class, and I find interfaces to be cleaner and
easier to maintain.

Que -: How to make class in Object Oriented Form? (in c#)

Eh? Like this?

class MyForm : System.Windows.Forms.Form
{
MyForm()
{
}
}



Happy coding!
Morten Wennevik [C# MVP]
 
B

Bonj of Beeeeeves

The interface is purely a specification, it doesn't actually
*do* anything. But specification-only code is often necessary.

Michael Grimsley said:
Cliff;

Thanks that clears quite a bit up. One last question, then why use an
Interface at all-if all it is saying is that you have to have these methods,
properties, etc., in the class.

Michael


Cliff Harris said:
When you define an interface, the methods within are never actually called.
An Interface just says, "Whoever inherits from me if guarenteed to have all
methods listed within me implemented."
When I implement an interface, all I am doing is telling everyone that I do,
in fact, contain the methods that are listed in the interface.
That way, my class, no matter what it does, can be cast into any Interface
that it implements without whatever is using my class knowing anything about
it.

This article:
http://msdn.microsoft.com/library/d...ry/en-us/csref/html/vcreftheinterfacetype.asp
Describes an interface as a "contract" and a class that implements an
interface is saying that it ahere's to the contract.... maybe the article
will explain it better...

I hope this was helpfull rather than more confusing...

-Cliff

Michael Grimsley said:
Morten;

Thanks for getting back to me. I still do not follow what is going on. You
are still defining the method twice, once in the interface that does nothing
and once in your class that does the work. So when you want to use the
method, which method is called, the one in the interface that no work
or
the
method where you defined it to do some work?

Thanks

Michael


Hi Michael,

I answered this very question about a week ago (well involving abstract
classes), so I'll just copy and paste the answer here. There is more to
interfaces than my answer tells you, but it might give you an idea
what
they
can be used for.

The following is taken from an earlier post
-------------------------------------------

Dear Friends ,
Please tell me following thing -
Que -: what is the main use of interface in .net ?

Interfaces are used to pass objects that are completely different
but
who
all have some methods that are defined in the interface.

Consider a class Rock and a class House. Completely different, and
nothing in common, almost. For instance, they both can drop down and crash,
say in case of an earthquake :p. Now, you have a class
Earthquake with a SmashObjects method. Instead of having to create a
parent class for everything on earth that can drop, and have every class,
like Rock and House inherit that class, you can make an
interface IDroppable and have Rock and House implement that
interface.
In
the interface you define Drop(); and now SmashObject can smash every object
that implements IDroppable because it knows, no
matter how different, they all have a Drop method.

interface IDroppable
{
Drop();
}

class House : IDroppable
{
Drop()
{
// do house dropping stuff
}
}

class Rock : Lava, IDroppable
{
Drop()
{
// do rock dropping stuff
}
}

class Earthquake
{
SmashObjects(IDroppable thingy)
{
thingy.Drop();
}
}

Now, in this sample you could have accomplished the same by making a base
parent class and overriding the Drop method, but what if the earthquake
wants to roll something. Now, the rock can roll, but
not the house (not really), but a car can roll. So, make a new interface
IRollable and have the rock and car implement that interface. You can only
inherit a single class, but implement numerous
interfaces. So basically, what I am trying to safe, interfaces describe
some common abilities. Oh, and the I in front of interfaces is intentional
to distinguish them from inherited classes.

Que -: What is difference between abstract class and interface?

An abstract class is a blueprint for a new class, it will act in a similar
way as a single interface, but as mentioned above, you can only
inherit
one
class, and I find interfaces to be cleaner and
easier to maintain.

Que -: How to make class in Object Oriented Form? (in c#)

Eh? Like this?

class MyForm : System.Windows.Forms.Form
{
MyForm()
{
}
}



Happy coding!
Morten Wennevik [C# MVP]
 
J

Jeff Louie

Michael... An interface is simply a contract without the implementation
details. You program to the contract and the supplier provides the black
box
implementation details. An example is a quicksort routine that requires
a
client specifed compare(obj1, obj2) implementation. You can reuse the
eneric
quicksort algorithm by providing a custom compare method appropriate to
your class. In C, you could use a callback to accomplish a similar
functionality. See IComparable for usage of an interface in C#.

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

Regards,
Jeff
Thanks that clears quite a bit up. One last question, then why use an
Interface at all-if all it is saying is that you have to have these
methods,
properties, etc., in the class.<
 
J

John Baro

Michael Grimsley said:
Cliff;

Thanks that clears quite a bit up. One last question, then why use an
Interface at all-if all it is saying is that you have to have these methods,
properties, etc., in the class.

You might define an interface that you would make all your classes
implement.
Lets say you have an interface I that defines a string property "Name"

You have class A and B which implement interface I.

You can define a method GetName and as a parameter you pass an interface
instead of a class.
Then you can simply call Parameter.Name, without knowing (or caring) about
the type of object that was passed to you.
All you know is that it implements interface I and you can call any methods
defined in interface I.

HTH
JB
Michael


Cliff Harris said:
When you define an interface, the methods within are never actually called.
An Interface just says, "Whoever inherits from me if guarenteed to have all
methods listed within me implemented."
When I implement an interface, all I am doing is telling everyone that I do,
in fact, contain the methods that are listed in the interface.
That way, my class, no matter what it does, can be cast into any Interface
that it implements without whatever is using my class knowing anything about
it.

This article:
http://msdn.microsoft.com/library/d...ry/en-us/csref/html/vcreftheinterfacetype.asp
Describes an interface as a "contract" and a class that implements an
interface is saying that it ahere's to the contract.... maybe the article
will explain it better...

I hope this was helpfull rather than more confusing...

-Cliff

Michael Grimsley said:
Morten;

Thanks for getting back to me. I still do not follow what is going on. You
are still defining the method twice, once in the interface that does nothing
and once in your class that does the work. So when you want to use the
method, which method is called, the one in the interface that no work
or
the
method where you defined it to do some work?

Thanks

Michael


Hi Michael,

I answered this very question about a week ago (well involving abstract
classes), so I'll just copy and paste the answer here. There is more to
interfaces than my answer tells you, but it might give you an idea
what
they
can be used for.

The following is taken from an earlier post
-------------------------------------------

Dear Friends ,
Please tell me following thing -
Que -: what is the main use of interface in .net ?

Interfaces are used to pass objects that are completely different
but
who
all have some methods that are defined in the interface.

Consider a class Rock and a class House. Completely different, and
nothing in common, almost. For instance, they both can drop down and crash,
say in case of an earthquake :p. Now, you have a class
Earthquake with a SmashObjects method. Instead of having to create a
parent class for everything on earth that can drop, and have every class,
like Rock and House inherit that class, you can make an
interface IDroppable and have Rock and House implement that
interface.
In
the interface you define Drop(); and now SmashObject can smash every object
that implements IDroppable because it knows, no
matter how different, they all have a Drop method.

interface IDroppable
{
Drop();
}

class House : IDroppable
{
Drop()
{
// do house dropping stuff
}
}

class Rock : Lava, IDroppable
{
Drop()
{
// do rock dropping stuff
}
}

class Earthquake
{
SmashObjects(IDroppable thingy)
{
thingy.Drop();
}
}

Now, in this sample you could have accomplished the same by making a base
parent class and overriding the Drop method, but what if the earthquake
wants to roll something. Now, the rock can roll, but
not the house (not really), but a car can roll. So, make a new interface
IRollable and have the rock and car implement that interface. You can only
inherit a single class, but implement numerous
interfaces. So basically, what I am trying to safe, interfaces describe
some common abilities. Oh, and the I in front of interfaces is intentional
to distinguish them from inherited classes.

Que -: What is difference between abstract class and interface?

An abstract class is a blueprint for a new class, it will act in a similar
way as a single interface, but as mentioned above, you can only
inherit
one
class, and I find interfaces to be cleaner and
easier to maintain.

Que -: How to make class in Object Oriented Form? (in c#)

Eh? Like this?

class MyForm : System.Windows.Forms.Form
{
MyForm()
{
}
}



Happy coding!
Morten Wennevik [C# MVP]
 
M

Morten Wennevik

Cliff;

Thanks that clears quite a bit up. One last question, then why use an
Interface at all-if all it is saying is that you have to have these methods,
properties, etc., in the class.

Michael

Because without interfaces you would need a whole set of overloaded methods handling different classes. If all the classes inherit one and the same interface with the necessary methods listed inside, all you need to do is to create a single method using the interface as a parameter. In my example you could create a method handling Houses, and another for Rocks and another ... but since they use the interface IDroppable describing a Drop method, and Earthquake's SmashObjects method only needs to call the Drop function, SmashObjects can use IDroppable as a parameter. It will know that any object that can be cast into IDroppable also has the Drop method implemented.


Happy coding!
Morten Wennevik [C# MVP]
 

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

Top