-1

Possible Duplicate:
Why would I want to use Interfaces? Why I need Interface?

I want to know where and when to use it?

For example,

interface IDemo
{
 // Function prototype
 public void Show();
}

// First class using the interface
class MyClass1 : IDemo
{
 public void show()
 {
  // Function body comes here
  Response.Write("I'm in MyClass");
 } 
}

// Second class using the interface
class MyClass2 : IDemo
{
 public void show() 
 {
  // Function body comes here
  Response.Write("I'm in MyClass2");
  Response.Write("So, what?");
 }

These two classes has the same function name with different body. This can be even achieved without Interface. Then why we need an Interface where and when to use it?

Community
  • 1
  • 1
Kamal Durai
  • 155
  • 3
  • 9
  • 2
    Please use the code formatting button `{ }`. – Marlon Aug 05 '12 at 06:17
  • 2
    Your example is too generic. You don't model inheritance just because two classes have similar method names. – Eranga Aug 05 '12 at 06:22
  • 1
    Why was this closed as exact duplicate **to questions that are themselves closed**? – Eric J. Aug 05 '12 at 06:29
  • 2
    @EricJ.: that's not a bad thing. Increases the web of links, so just by "walking the chain" of possible dups and related linked pages, you get to see quite a few questions - hopefully one of them (and its answers) will be phrased in a way that helps the OP. – Mat Aug 05 '12 at 06:31

3 Answers3

1

In your simple case, you could achieve something similar to what you get with interfaces by using a common base class that implements show() (or perhaps defines it as abstract). Let me change your generic names to something more concrete, Eagle and Hawk instead of MyClass1 and MyClass2. In that case you could write code like

Bird bird = GetMeAnInstanceOfABird(someCriteriaForSelectingASpecificKindOfBird);
bird.Fly(Direction.South, Speed.CruisingSpeed);

That lets you write code that can handle anything that is a *Bird*. You could then write code that causes the Bird to do it's thing (fly, eat, lay eggs, and so forth) that acts on an instance it treats as a Bird. That code would work whether Bird is really an Eagle, Hawk, or anything else that derives from Bird.

That paradigm starts to get messy, though, when you don't have a true is a relationship. Say you want to write code that flies things around in the sky. If you write that code to accept a Bird base class, it suddenly becomes hard to evolve that code to work on a JumboJet instance, because while a Bird and a JumboJet can certainly both fly, a JumboJet is most certainly not a Bird.

Enter the interface.

What Bird (and Eagle, and Hawk) do have in common is that they can all fly. If you write the above code instead to act on an interface, IFly, that code can be applied to anything that provides an implementation to that interface.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
0

Here are a couple of good links.

http://www.daniweb.com/software-development/csharp/threads/114364/why-use-interfaces

http://fci-h.blogspot.com/2008/03/oop-design-concepts-interfaces_05.html

Elaboration: Basically they provide more abstraction. If you have an object say Alien and in this case all Aliens are from outer space. Well not all Aliens are exactly the same but they all consume food and use energy. The way they consume food and use energy may be different but to keep a base Alien class and have interfaces that abstract away from that class makes more sense than to have separate classes for each type.

Core logic can be kept the same in the base class and interfaces can change parts when needed.

edhedges
  • 2,722
  • 2
  • 28
  • 61
  • The reason I was lazy is because anyone can do a quick google and read an explanation from there about "Why interfaces". – edhedges Aug 05 '12 at 06:29
0

Interfaces are mostly needed when you have components that are dependent on one another. A common example is a logging controller and some logger classes.

class LoggingController {

    private ILogger _logger

    // expecting an interface here removes the dependency 
    // to a specific implemenentation.
    // all it cares about is that the object has a Log() method
    public LoggingController(ILogger logger) {
        _logger = logger;
    }

    public void Log() { _logger.Log(); }
}

interface ILogger { 
    void Log(); 
}

class DbLogger : ILogger { 
    public void Log(){ //log into db }
}

class TxtLogger : ILogger {
    public void Log(){ //log into a txt file }
}

At runtime, the logging controller can be injected with any implementation of the ILogger, and it is completely unaware of what the logger actually does to actually log stuff.

The other benefit of programming to interfaces is that it allows for easier unit testing. The controller can easily be unit tested by injecting a mock logger which would also implement the interface.

Ivan Pintar
  • 1,861
  • 1
  • 15
  • 27
  • Can I ask why this was downvoted? If there is something that is wrong, I'd much prefer an explanation and correction... – Ivan Pintar Aug 05 '12 at 06:31