C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-04-2009, 09:23 AM   #1
Registered User
 
Join Date: Jul 2009
Posts: 5
interfaces c# what the point?

Just been reading a chapter on Interfaces; what's the point of adding an interface to your class? Considering all i get for using them is a bunch of methods which i have to implement myself.

So what's the purpose? what can they be used for? as per usual the book only gives pointless code examples and no real explanation of why you would use them.

thanks.
taver is offline   Reply With Quote
Old 10-04-2009, 11:25 AM   #2
&TH of undefined behavior
 
Fordy's Avatar
 
Join Date: Aug 2001
Posts: 5,183
An interface is a contract. It says that any user of your code and expect the following methods. That interface might be used to read data and you might have written a class to get that data from a text file. Later you might recode that class into to pull data from a database or a remote procedure call. Later again you might decide to completely rewrite the code to make it faster and more robust.

As long as you use the same interface, the users of your code don't need to know about what you are doing because no matter what's going on behind the scenes, you expose the same methods via that initial interface.

It gives the users of your code an assurance that they wont have to completely recode their programs due to changes you make.
__________________
"If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut."
Albert Einstein (1879 - 1955)


Board Rules
Fordy is offline   Reply With Quote
Old 10-04-2009, 12:31 PM   #3
Registered User
 
Join Date: Jul 2009
Posts: 5
thanks for reply, so it's only to ensure the way the methods used do not change? Is this used a lot in the industry, i mean using the language Interface ability?
taver is offline   Reply With Quote
Old 10-04-2009, 02:56 PM   #4
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
More importantly it can give multiple different classes the same type of operations. A (naive) way of supporting multiple types of printers could be done like this:
Code:
public class FilePrinter
{
  public void PrintToFile();
}

public class NetworkPrinter
{
  public PrintOverNetwork();
}

public void Print(object printer)
{
  if(printer is FilePrinter)
  {
    var file_printer = printer as FilePrinter;
    file_printer.PrintToFile();
  }
  else if(printer is NetworkPrinter)
  {
    var network_printer = printer as NetworkPrinter;
    network_printer.PrintOverNetwork();
  }
}
A better way would be to give them a common printing interface:
Code:
public interface IPrinter
{
  void Print();
}

public class FilePrinter : IPrinter
{
  public void PrintToFile();
  public void Print() { PrintToFile(); }
}

public class NetworkPrinter : IPrinter
{
  public PrintOverNetwork();
  public void Print() { PrintOverNetwork(); }
}

public void Print(IPrinter printer)
{
  printer.Print();
}
A cleaner solution, less cluttering if-elses and much easier to extend with more Printers later (maybe an USBPrinter?).
__________________
MagosX.com

Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Magos is offline   Reply With Quote
Old 10-04-2009, 05:19 PM   #5
Registered User
 
Join Date: Mar 2009
Location: england
Posts: 76
Only time I ever used interfaces was when I was playing around with .net plugins. Having an interface of methods and variables is really cool if you want to add expandability to your application through .net dll plugins and so on.
theoobe is offline   Reply With Quote
Old 10-05-2009, 09:34 AM   #6
Ugly C Lover
 
audinue's Avatar
 
Join Date: Jun 2008
Location: Indonesia
Posts: 462
Why no abstract class?
__________________
Just GET it OFF out my mind!!
audinue is offline   Reply With Quote
Old 10-05-2009, 10:00 AM   #7
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
Quote:
Originally Posted by audinue
Why no abstract class?
What exactly do you mean by that question? For example, are you asking why an interface should be used instead of an abstract class, or do you think that C# does not have abstract classes as a language feature?
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 10-05-2009, 10:11 AM   #8
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
Quote:
Originally Posted by audinue View Post
Why no abstract class?
Well
__________________
MagosX.com

Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Magos is offline   Reply With Quote
Old 10-06-2009, 04:14 AM   #9
Registered User
 
Join Date: Jul 2009
Posts: 5
Ok, why use an interface instead of an abstract class?
taver is offline   Reply With Quote
Old 10-06-2009, 04:19 AM   #10
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
Recommendations for Abstract Classes vs. Interfaces
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting a floating point exception SnertyStan C Programming 13 03-25-2008 11:00 AM
How to monitor process creation? markiz Windows Programming 31 03-17-2008 02:39 PM
instantiation point discussion George2 C++ Programming 1 03-08-2008 03:48 AM
Drawing Program Max_Payne C++ Programming 21 12-21-2007 05:34 PM
overloading operator help syrel C++ Programming 8 09-30-2004 10:49 AM


All times are GMT -6. The time now is 01:09 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22