473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 15725
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
6
by: John Salerno | last post by:
I understand how they work (basically), but I think maybe the examples I'm reading are too elementary to really show their value. Here's one from Programming C#: #region Using directives ...
6
by: Ricky W. Hunt | last post by:
It's dawning on my a lot of my problems with VB.NET is I'm still approaching it in the same way I've programmed since the late 70's. I've always been very structured, flow-charted everything, used...
1
by: Rich | last post by:
Hello, my project (vb2005) contains several classes that each produce lists of data which get stored/displayed in ado.net tables that have the same structure for each of the lists produced by...
17
by: Zytan | last post by:
Aren't all classes interfaces? What constitutes an interface (and with it, the "I" prefix distinction)? Zytan
2
by: Lilith | last post by:
This is something I can't seem to find in my books, which may be a bit antiquated. Two examples: virtual unsigned long size ()=0; and virtual void operator () ()=0; I'm looking to find...
8
by: rn5a | last post by:
Suppose I have the following class code: Imports System Imports System.Data Imports System.Data.SqlClient Public Class DBSettings Private sqlCmd As SqlCommand Private sqlConn As...
8
by: gz | last post by:
Hi All, It is my understanding that with .NET you can add or remove methods in your interface while still maintain binary compatibility. This is not true for COM. Does this mean that published...
2
by: Tony Johansson | last post by:
Hello! Below I have a working program. I have one generic class called Farm<T> with this header definition public class Farm<T: IEnumerable<Twhere T : Animal Now to my question I changed the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.