473,386 Members | 1,883 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,386 software developers and data experts.

Help on Question on Interface

Dear all,

I read many documents and article based on Interface, but
I am still blind with that and start to have no more hairs.

I try to clearly understand when should I create
Interface ?

thanks to clear my stupid mind
regards
serge
Jul 19 '05 #1
2 1729
Hi Serge.
I usually view Interfaces as a way to provide "abilities" to certain
objects.
for example, if I want an object to be able to look like an Animal, I would
create an interface that would describe an animal, and name it
appropriately. for example - "IAnimal".
the "I" in the beginning denotes that this is an Interface.

Let's say to an animal, in my case would be able to eat and drink.
So in the interface I would create two methods name "Eat()" and "Drink()".

Now, every object that would like to be able to become an animal(or
represent itself as an animal) must implement this interface, meaning it
must implement "Drink()" and "Eat()" and should be marked as implementing
IAnimal .
in C# you would write:

public Interface IAnimal
{
void Eat()
{//no code here}

void Drink()
{//no code here}
}

public class MyObject : IAnimal
{
void Eat()
{
//do some eating code here
}

void Drink()
{
//do some drinking code here
}

}

now. because this is an interface and each class can implement it
differently, we only write the code of the methods of the interface inside
the classes that implement that interface.
So for example, I could have a "Dog" class and a "Cat" class and they would
both implement IAnimal.
Each one can implement the eat and drink methods differently, but because
they both implement the IAnimal interface, I can write generic code like
this to handle both types of classes with very simple code:
//notice that I receive the Interface, not an actual class
void MakeAnimalDrinkAndEat(IAnimal animal)
{
//I call the actual class that implements the interface here
animal.Drink()
animal.Eat()
}

void main()
{
Dog MyDog = new Dog();
Cat MyCat = new Cat();

//notice that I don't care what kind of class I send to that
//method as long as that class implements the IAnimal interface
MakeAnimalDrinkAndEat(MyDog)
MakeAnimalDrinkAndEat(MyCat)
}
The behavior shown here is called "Polymorphism" - the ability of an object
to appear in different shapes. Implementing interfaces allows this.
In this case the "MyDog" class instance was able to show itself both as a
"Dog" class, and both as an "IAnimal".

Now, see how powerful this is: If I now create a new object, let's say
"Elephant", and I want it to be able to eat and drink and being treated like
an Animal - I make it implement IAnimal, and I can send that object to the
"MakeAnimalDrinkAndEat" method. I *gave* it the ability to be an animal.

This is it on a very quick discussion.
For more about interfaces see this:
http://www.superdotnet.com/show_article.aspx?pkID=136

--
Regards,

Roy Osherove
http://www.iserializable.com
---------------------------------------------

"Serge Calderara" <se*************@maillefer.net> wrote in message
news:09****************************@phx.gbl...
Dear all,

I read many documents and article based on Interface, but
I am still blind with that and start to have no more hairs.

I try to clearly understand when should I create
Interface ?

thanks to clear my stupid mind
regards
serge

Jul 19 '05 #2

Wowww thanks again for that details explaination...
But as there are different thinking on this interface from different
people I am confuse. thats why I ask different samples.

Now ahat is your commnets on the following way :

I create a class named Animal with overidable function Read and Eat
Then Dog or Cat or Elephnat if they want to act as an animal must
inherits from that Animal class and can then overide Eat and Drink
function accordingly to them.

Sounds that I get same functionality as an IAnimal interface here?

If not why? if Yes why the defining IAnimal?

thanks for your commnets
Serge

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Liza | last post by:
Hi guys, i'm trying to build a web service....... is there such thing as polymorphism in web services? i mean could i have two web services of the same name but different arguments such that i...
3
by: glenn | last post by:
Can anyone tell me what resource you use to get help with C# VS? or VS in general? I have been programming in Delphi for 10 years and am just getting started with VS and I have to say that the...
4
by: Serge Calderara | last post by:
Dear all, I read many documents and article based on Interface, but I am still blind with that and start to have no more hairs. I try to clearly understand when should I create Interface ? ...
2
by: Andrew S. Giles | last post by:
OK, Ive run my head into this wall for too long. I need help. I am developing an applicaiton in C# to present a user with a GUI to specify a configurable list of machines that he wants to listen...
8
by: Alison | last post by:
Hi, Al I am trying to design a user interface which provides both menus and toolbars for some users to click on whatever they want to do, at the same time, I would like to have a console window...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
4
by: Andrew Taylor | last post by:
Hi, I've been using PHP for a long time, I have designed and developed a number of mid-range systems. I've always used a procedural approach. I fully understand the concept of OO, I know all the...
1
by: dupe576 | last post by:
Hey, My boss put me in charge of getting this pin pad device to work with our software. We have it plugging into the usb port with a converter cable that converts it to a rs232 port. This...
4
by: sip.address | last post by:
Hi there, When creating interfaces and implementations, the usual thing is doing somethign like class Interface { public: virtual void f() = 0; virtual void g() = 0; };
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.