473,396 Members | 2,030 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.

Interfaces (NOOBIE)

ME
I am learning C#. I can't seem to understand why one would use an
interface, it doesn't seem to do anything at all. Why not just leave the
interface out and just code the properties and what not with out them? I
have found numerous articles on the subject but I can't seem to find one
that gives me a programming example of WHY. Plenty out there of how. Just
need to know WHY. Please try to keep it simple I am kind of slow when it
comes to this stuff.

Kind Regards,

Matt
Nov 15 '05 #1
3 1011
Say you have a class called circle and a class called rectangle.
They are completely different, one has corners, the other is smooth, and
so on.
Yet, you have a method that takes either a circle or an rectangle and uses
their Volume or Surface property.
If you make an IGeometricObject interface (always use I to indicate
interface) you can pass any class "subscribing" to IGeometricObject and
not worry about wether the object is rectangle or circle.

interface IGeometricObject
{
public float GetVolume(); // a method all subscribers will have to
implement
public float GetSurface(); // anther such method
}

class Rectangle : IGeometricObject
{

public float GetVolume()
{
// TODO: Add Rectangle.GetVolume implementation
return 0;
}

public float GetSurface()
{
// TODO: Add Rectangle.GetSurface implementation
return 0;
}
}
class Circle : IGeometricObject
{

public float GetVolume()
{
// TODO: Add Circle.GetVolume implementation
return 0;
}

public float GetSurface()
{
// TODO: Add Circle.GetSurface implementation
return 0;
}
}

class SomeClass
{
public SomeClass()
{
Circle aCircle = new Circle();
Rectangle aRectangel = new Rectangle();

float number = calculateStuff(aCircle) + calculateStuff(aRectangle);
}

private float calculateStuff(IGeometricObject geo)
{
return geo.GetVolume() * geo.GetSurface();
}
}

So, if you have two different classes, but they both share some common
stuff, or you want them to share something (like being sortable in an
ArrayList, just "subscribe" to IComparer and/or IComparable) you use
interfaces.

You may look upon interfaces as building blocks for your class.
You have your basic class, maybe inherited from another class, and then
you add several interfaces to make it even more useful.

Visual Studio .Net will even fill in the required methods by pressing the
tab key when you add an interface.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #2
A good example is sorting objects (in arrays and lists etc). The
documentation just tells you to implement IComparable, which only has a
single method that compares two objects.

So by doing this you can sort objects that are in totally different class
hierarchies - they don't need to have a common ancestor that has a
CompareTo() method, as each can implement its own.

Regards

Ron

"ME" <trash.trash@comcastdotnet> wrote in message
news:Wd********************@comcast.com...
I am learning C#. I can't seem to understand why one would use an
interface, it doesn't seem to do anything at all. Why not just leave the
interface out and just code the properties and what not with out them? I
have found numerous articles on the subject but I can't seem to find one
that gives me a programming example of WHY. Plenty out there of how. Just need to know WHY. Please try to keep it simple I am kind of slow when it
comes to this stuff.

Kind Regards,

Matt

Nov 15 '05 #3
Ch 8 of Bruce Eckel's "Thinking in C#" may give you
your reason. Sorry, i can't find a link to a d/lable copy.
[and the book has had a rather checkered history].

--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
"ME" <trash.trash@comcastdotnet> wrote in message
news:Wd********************@comcast.com...
I am learning C#. I can't seem to understand why one would use an
interface, it doesn't seem to do anything at all. Why not just leave the
interface out and just code the properties and what not with out them? I
have found numerous articles on the subject but I can't seem to find one
that gives me a programming example of WHY. Plenty out there of how. Just need to know WHY. Please try to keep it simple I am kind of slow when it
comes to this stuff.

Kind Regards,

Matt

Nov 15 '05 #4

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

Similar topics

2
by: osgnamah | last post by:
Hi All; I am getting ready to launch a shareware program in the next few weeks and the last step is going to be creating a website. So the last few days I've been doing some web surfing in...
1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
6
by: Aristotelis E. Charalampakis | last post by:
Hi all, this is a newbie question :-) I was wondering if there was a way to use the switch statement in a manner that each case statement includes more that a simple value. i.e.: switch (...
30
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
8
by: John | last post by:
What is the purpose / benefit of using an interface statement? It doesn't seem like anything more than a different way to make a class... (except you can't define any procedures in an interface...
18
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller...
22
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to...
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
23
by: A.Gallus | last post by:
If I declare a function pure virtual: class A { virtual void myfunc() = 0; } and I derive a class from A: class B : public A
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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...
0
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,...
0
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...

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.