locked
Why Interface is needed? RRS feed

  • Question

  • Why Interface is needed?
    What is its use?
    how to it differ from other's(abstract class)?
    Thanks, Bharathi
    Friday, July 24, 2009 1:38 PM

Answers

  • Read

    http://codeofdoom.com/wordpress/2009/02/12/learn-this-when-to-use-an-abstract-class-and-an-interface/
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by Bin-ze Zhao Monday, July 27, 2009 11:00 AM
    Friday, July 24, 2009 1:41 PM
  • Well.

    Eric is a Person.
    BlackWolf is an Animal.

    Both are reproducible.

    If you want to simulate a live-being reproduction cycle, You

    1) write an IReproductible interface (having a DoReproduction() method);
    2) inherits Person and Animal from IReproducible ;
    2) put both Eric, Eva and BlackWolf and GrayWolf in a List<object> - myListOfBeings, you can add here also other (non-beings) objects for diversity;


    foreach (object o in myListOfBeings)
    {
        if (o is IReproducible)                                  // search only reproducible objects
            (o as IReproducible).DoReproduction();  // other that IReproducible objects are not guaranteed to have DoReproduction() method
    }

    Best regards, Sergiu
    • Marked as answer by Bin-ze Zhao Monday, July 27, 2009 10:59 AM
    Friday, July 24, 2009 4:27 PM

All replies

  • Read

    http://codeofdoom.com/wordpress/2009/02/12/learn-this-when-to-use-an-abstract-class-and-an-interface/
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by Bin-ze Zhao Monday, July 27, 2009 11:00 AM
    Friday, July 24, 2009 1:41 PM
  • It guarantees that a certain named behavior will be available to all that use the interface but allows the details of said behavior to be defined in the derived class.
    Friday, July 24, 2009 1:43 PM
  • Interfaces are useful when you have classes that share an element of common functionality, but are otherwise unrelated. For example many classes implement the IDisposable interface allowing you to iterate through a collection of these classes and call class.Dispose() on each of them, providing they are casted as the interface. 

    While an abstract class is used more for providing a base functionality for related classes.
    Friday, July 24, 2009 1:54 PM
  • I like the example of the Strategy Pattern in Head First Design Patterns because in that example it uses both the abstract class and interaces.

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace StrategyPattern
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Mallard Duck
                Duck mallard = new MallardDuck();
                mallard.Display();           
                mallard.PerformFly();
                mallard.PerformQuack();

                Console.WriteLine("");

                //Decoy Duck
                Duck decoy = new DecoyDuck();
                decoy.Display();
                decoy.PerformFly();
                decoy.PerformQuack();
                Console.ReadLine();
            }
        }

       #region Interfaces

        interface IFlyBehavior
        {
            void Fly();
        }

        interface IQuackBehavior
        {
            void Quack();
        }

       #endregion

       #region Duck Class

        abstract class Duck
        {
            protected IFlyBehavior flyBehavior;
            protected IQuackBehavior quachBehavior;

            public abstract void Display();

            public void Swim()
            {
               Console.WriteLine("I am swimming");
            }

            public void PerformFly()
            {
                flyBehavior.Fly();
            }

            public void PerformQuack()
            {
                quachBehavior.Quack();
            }

            public void SetFlyBehavior(IFlyBehavior fb)
            {
                flyBehavior = fb;
            }

            public void SetQuackBehavior(IQuackBehavior qb)
            {
                quachBehavior = qb;
            }
        }

       #endregion

       #region Behaviors

        class FlyWithWings : IFlyBehavior
        {
            public void Fly()
            {
                Console.WriteLine("I am flying with wings");
            }
        }

        class FlyNoWay : IFlyBehavior
        {
            public void Fly()
            {
                Console.WriteLine("I cannot fly");
            }
        }

        class Quacking : IQuackBehavior
        {
            public void Quack()
            {
                Console.WriteLine("I am quacking");
            }
        }

        class MuteQuack : IQuackBehavior
        {
            public void Quack()
            {
                Console.WriteLine("I cannot quack");
            }
        }

        class Squeak : IQuackBehavior
        {
            public void Quack()
            {
                Console.WriteLine("I am squeaking");
            }
        }

       #endregion

       #region Ducks

        class MallardDuck : Duck
        {
            public MallardDuck()
            {
                flyBehavior = new FlyWithWings();
                quachBehavior = new Quacking();
            }

            public override void Display()
            {
                Console.WriteLine("I am a Mallard Duck");
            }
        }

        class DecoyDuck : Duck
        {
            public DecoyDuck()
            {
                flyBehavior = new FlyNoWay();
                quachBehavior = new MuteQuack();
            }

            public override void Display()
            {
                Console.WriteLine("I am a Decoy Duck");
            }
        }

       #endregion

    }


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Friday, July 24, 2009 1:57 PM
  • Well.

    Eric is a Person.
    BlackWolf is an Animal.

    Both are reproducible.

    If you want to simulate a live-being reproduction cycle, You

    1) write an IReproductible interface (having a DoReproduction() method);
    2) inherits Person and Animal from IReproducible ;
    2) put both Eric, Eva and BlackWolf and GrayWolf in a List<object> - myListOfBeings, you can add here also other (non-beings) objects for diversity;


    foreach (object o in myListOfBeings)
    {
        if (o is IReproducible)                                  // search only reproducible objects
            (o as IReproducible).DoReproduction();  // other that IReproducible objects are not guaranteed to have DoReproduction() method
    }

    Best regards, Sergiu
    • Marked as answer by Bin-ze Zhao Monday, July 27, 2009 10:59 AM
    Friday, July 24, 2009 4:27 PM
  • MUltiple Inheritance can be achieved ony through interfaces
    Thanks, Bharathi
    Tuesday, July 28, 2009 6:00 AM