469,623 Members | 937 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

What is the purpose of Interface

Dear Friends
Please tell me following thing
Que -: what is the main use of interface in .net
Que -: What is difference between abstract class and interface
Que -: How to make class in Object Oriented Form? (in c#

Please mail me at my mail id - ch****************@yahoo.co.in
Nov 16 '05 #1
3 15554
On Tue, 25 May 2004 02:06:02 -0700, Amit chaturvedi <General Question - amit chaturvedi> wrote:
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()
{
}
}

Please mail me at my mail id - ch****************@yahoo.co.in

No can do, I'm writing this over nntp at the moment, without smtp abilities. Maybe someone can forward it.
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
> Interfaces are used to pass objects that are completely different but who
all have some methods that are defined in the interface.

Thanks for en some simple but excellent examples.

Anders
Nov 16 '05 #3
Excellent comparison!

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

92 posts views Thread by Reed L. O'Brien | last post: by
6 posts views Thread by John Salerno | last post: by
6 posts views Thread by Ricky W. Hunt | last post: by
2 posts views Thread by Lilith | last post: by
8 posts views Thread by rn5a | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.