473,695 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trying to understand interfaces

RSH
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 a class, that when
implemented, they require the implementing calss to make sure that each of
the properties, functions etc. are handled by the class. (????)

What I am really having a problem with is how they overcome the limitation
of multiple inheritance? It would appear that interfaces simply require that
a class handles a predetermined number of properties events etc. So if I
have a class that implements an interface and I need to add functionality to
that class...I still have to add it to the interface and handle it in the
initial class...this seems redundant to me. Not to mention modifying the
interface to include the new required functionality, now makes all other
classes that implement the interface "break" and now require that I modify
all classes that implement the interface to handle the newly added
functionality.

Now I openly admit I am no OOP guru, and I am simply trying to understand
interfaces...I am certainly not bashing them.

The code below illustrates my initial attempt at understanding
interfaces...pi eced together from a few tutorials on the web.

The classes and intefaces apper below. and the instantiation of the objects
appears at the top...and will probably shed light on my lack of
understanding of interfaces.

The first instantiation illustrates the use of the interfaces:
public partial class _Default : System.Web.UI.P age

{

protected void Page_Load(objec t sender, EventArgs e)

{

IPencil p = new Pencil();

p.Type = "Pencil";

IPencil mp = new MechanicalPenci l();

mp.Type = "MechanicalPenc il";

IPencil pen = new Pen();

pen.Type = "Pen";

PencilSharpener sharpener = new PencilSharpener ();

sharpener.Sharp en(p);

sharpener.Sharp en(mp);

sharpener.Sharp en(pen);

}

Now where I get confused is that I can quite simply get the same results by
bypassing the interfaces and never calling the PencilSharpener class:

protected void Page_Load(objec t sender, EventArgs e)

{

IPencil p = new Pencil();

p.Type = "Pencil";

p.OnSharpened() ;

IPencil mp = new MechanicalPenci l();

mp.Type = "MechanicalPenc il";

IPencil pen = new Pen();

pen.Type = "Pen";

//PencilSharpener sharpener = new PencilSharpener ();

//sharpener.Sharp en(p);

//sharpener.Sharp en(mp);

//sharpener.Sharp en(pen);

}

So this is where my paridigm needs some shifting :-)

Can you please help shed some light on this? Thank you very much!

Ron


// Interfaces +++++++++++++++ +++++++++++++++ +++

//Pencil Interface

public interface IPencil

{

string Type { get; set; }

int CurrentSharpnes s { get; set; }

bool IsSharp { get; }

void Write();

void OnSharpened();

}

//Pencil Sharpener Interface

public interface IPencilSharpene r

{

void Sharpen(IPencil pencil);

}



// Classes +++++++++++++++ +++++++++++++++ +++++++

// Pencil Class

public class Pencil : IPencil

{

private string m_message;

private string m_sharpenedMess age;

private string m_type;

private int m_currentSharpn ess;

private int m_charsUsed;

private Boolean m_Sharpened = false;

// Constructor

public Pencil()

{

m_type = string.Empty;

m_currentSharpn ess = 0;

m_message = string.Empty;

m_sharpenedMess age = string.Empty;

m_charsUsed = 0;

}

// Property

public string Message

{

get { return m_message; }

}

// Property

public string SharpenedMessag e

{

get { return m_sharpenedMess age; }

}

// Property

public string Type

{

get { return m_type; }

set {

m_type = value;

m_message = "This is my " + m_type + " writing away!";

m_sharpenedMess age = "This is one sharp " + m_type + "!";

}

}

// Property

public int CurrentSharpnes s

{

get { return m_currentSharpn ess; }

set { m_currentSharpn ess = value; }

}

// Property

public bool IsSharp

{

get { return m_charsUsed <= m_currentSharpn ess; }

}

// Function

public void Write()

{

foreach (char c in m_message)

{

if (m_Sharpened == false)

{

if (IsSharp)

{

HttpContext.Cur rent.Response.W rite(c);

}

else

{

HttpContext.Cur rent.Response.W rite("#");

}

}

else

{

HttpContext.Cur rent.Response.W rite(c);

}

m_charsUsed++;

}

HttpContext.Cur rent.Response.W rite("<BR>");

}

// Function

public void OnSharpened()

{

while(this.m_cu rrentSharpness < m_message.Lengt h)

{

m_charsUsed = 0;

m_currentSharpn ess++;

Write();

}

m_Sharpened = true;

m_message = m_sharpenedMess age;

Write();

}

public void Sharpen(IPencil pencil)

{

pencil.OnSharpe ned();

}

}

// Pencil Sharpener Class

public class PencilSharpener : IPencilSharpene r

{

public void Sharpen(IPencil pencil)

{

HttpContext.Cur rent.Response.W rite("<br>Begin sharpening " + pencil.Type +
"...<br>");

pencil.OnSharpe ned();

}

}

// Pen Class

class Pen : IPencil

{

private string m_message;

private string m_sharpenedMess age;

private string m_type;

private int m_currentSharpn ess;

private int m_charsUsed;

private Boolean m_Sharpened = false;

// Constructor

public Pen()

{

m_type = string.Empty;

m_currentSharpn ess = 0;

m_message = string.Empty;

m_sharpenedMess age = string.Empty;

m_charsUsed = 0;

}

// Property

public string Message

{

get { return m_message; }

}

// Property

public string SharpenedMessag e

{

get { return m_sharpenedMess age; }

}

// Property

public string Type

{

get { return m_type; }

set { m_type = value; }

}

// Property

public int CurrentSharpnes s

{

get { return m_currentSharpn ess; }

set { m_currentSharpn ess = 0; }

}

// Property

public bool IsSharp

{

get { return m_charsUsed <= m_currentSharpn ess; }

}

public void Write()

{

HttpContext.Cur rent.Response.W rite(m_message + "<BR>");

}

// Function

public void OnSharpened()

{

m_Sharpened = true;

m_message = "A Pen cannot be sharpened!.";

Write();

}

}

// Mechanical Pencil Class

class MechanicalPenci l : IPencil, IPencilSharpene r

{

private string m_message;

private string m_sharpenedMess age;

private string m_type;

private int m_currentSharpn ess;

private int m_charsUsed;

private Boolean m_Sharpened = false;

// Constructor

public MechanicalPenci l()

{

m_type = string.Empty;

m_currentSharpn ess = 0;

m_message = string.Empty;

m_sharpenedMess age = string.Empty;

m_charsUsed = 0;

}

// Property

public string Message

{

get { return m_message; }

}

// Property

public string SharpenedMessag e

{

get { return m_sharpenedMess age; }

}

// Property

public string Type

{

get { return m_type; }

set { m_type = value; }

}

// Property

public int CurrentSharpnes s

{

get { return m_currentSharpn ess; }

set { m_currentSharpn ess = value; }

}

// Property

public bool IsSharp

{

get { return m_charsUsed <= m_currentSharpn ess; }

}

// Function

public void Write()

{

foreach (char c in m_message)

{

if (m_Sharpened == false)

{

if (IsSharp)

{

HttpContext.Cur rent.Response.W rite(c);

}

else

{

HttpContext.Cur rent.Response.W rite("#");

}

}

else

{

HttpContext.Cur rent.Response.W rite(c);

}

m_charsUsed++;

}

HttpContext.Cur rent.Response.W rite("<BR>");

}

// Function

public void OnSharpened()

{

m_Sharpened = true;

m_message = "The Mechanical Pencil is self sharpening.";

Write();

}

public void Sharpen(IPencil pencil)

{

}

}

}


Oct 20 '06
22 2101
BTW, disprove definitely has only one O ;)

--
Dave Sexton

"Laurent Bugnion" <ga*********@bl uewin.chwrote in message
news:uc******** ******@TK2MSFTN GP05.phx.gbl...
Hi,

Dave Sexton wrote:
>Hi Mark,

I'm glad to hear that you agree with me on the point I made about the OP's
use of interfaces :)

But in our conversation I wasn't looking for agreement just for the sake of
agreeing, but instead for the sake of learning. When a conversation ends
without agreement, everyone's left with one or more competing ideas and no
solution, although I think we located the root of our difference of opinion
anyway.

I think there can be learning without agreement (unless I misunderstand what
agreement really means in english). I still don't really agree with Mark's
thoughts about documenting the source code, but I have thought *a lot* about
it since we talked about that, and I think it will help me to be more
efficient about my coding.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch

Oct 23 '06 #21
On Mon, 23 Oct 2006 21:53:10 +0200, Laurent Bugnion
<ga*********@bl uewin.chwrote:
>I think there can be learning without agreement
I disagree! <vbg>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 23 '06 #22
Hi,

Ben Newsam wrote:
On Mon, 23 Oct 2006 21:53:10 +0200, Laurent Bugnion
<ga*********@bl uewin.chwrote:
>I think there can be learning without agreement

I disagree! <vbg>
Did you at least learn something? ;-)

Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Oct 24 '06 #23

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

Similar topics

10
1514
by: Tom | last post by:
Hello, I've recently started trying to learn C (for the fun of it!). I have been using Perl and other languages for many years, but they were always very high level, this is giving me quite a ride. Here is my situation: I have the following code: ////////////////////////////////////////////////////////////
6
1921
by: dotNeter | last post by:
The services, talked here, are things used in IServiceContainer, IServiceProvider, etc. In my opinion, everything can be a service, and a service is generally used for providing specific features for service consumers, at design time. But, I think the cnsumers must completely know all aspects of that service. This sounds not good, and breaks the decoupling rule. So how to understand the role of Service, in a software engineering...
27
3845
by: jm | last post by:
I am having trouble understanding the purposes of an interface, even though the concept of interfaces is around me all the time (user interface, for example). I'm just not understanding software interfaces. Like anything else, it appears Interfaces are by design something that requires documentation. I know this may be obvious, but if I have a class A and I say, well, class A implements IMyInterface. The fact that it implements...
0
8631
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
8572
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
8984
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
8847
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
7661
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...
0
5839
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2272
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.