473,786 Members | 2,350 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1762
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 MakeAnimalDrink AndEat(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
MakeAnimalDrink AndEat(MyDog)
MakeAnimalDrink AndEat(MyCat)
}
The behavior shown here is called "Polymorphi sm" - 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
"MakeAnimalDrin kAndEat" 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
1586
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 could construct the following use case Actor : WebServiceRequestor
3
1488
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 level of community help pales in comparison. Borland seems to have many more people more than willing to help and this forum seems a bit slow and rude. There have been very few cases where I"ve gotten help here and even then it seems to take a...
4
368
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 ? thanks to clear my stupid mind regards
2
1993
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 to the output of. Specify a filename to shove all of the data (into Excel), and start the whole thing going. I get that done no problem. The problem comes with the Data. The data is coming from a different application, and I am not 100% sure of...
8
18658
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 available in the same form for users to enter commands and display outputs if some prefer to use character based user interface. I would like to implement the user interface in vb .net. Now I have menus and toolbars ready, but do not now how to get a...
7
4474
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 to be checking that the correct data type is used DataAcess sets an abstract class and methods
4
1635
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 basics and I know how to code OO. What I'm having problems with is understanding how to design/architect projects using OO. Can anyone reccomend any good online tutorials / online learning / video tutorials which deal with these subjects...
1
2100
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 converter cable uses the pl2303 driver to aid with this. I am new to drivers, particularly on the windows OS. My question is how do I use this driver to communicate with the device? I'm trying to read into it but am being overloaded by...
4
1627
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
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9492
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10360
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.