locked
what is Interface ?what it's benefits? or Why we Use interface ? RRS feed

  • Question

  • When I Use the Class Diagram I Found That there are some tools such as : Class,Enum,Interface,Module,Inhertance and others tools .
    I Want to Ask about interface ,what is Interface ?what it's benefits? or Why we Use interface ?
    Monday, December 29, 2008 2:14 PM

Answers

All replies

  • Hi Dear ,

      

         For a better programming Interface in most impotant concept .

     

      Interface : It  contains only Function Prototype (Function Declaration)

     

      Uses : To Implement  Multiple inheritance.

     

      Why we use ?

       >>> most of  time we use interface to declare common object . so we can easily access  common functionality  into different classes.

     

      >>> example :  painting is the common functionality in all kind of vehicle.

     

      interface Paint

     {

       

       public void Choose_Color();

        

     }

     

    class car : Paint

     {

        public void Choose_Color()

         {

                ***************** Your Logic******

         }

      }

     

    class bus : Paint

    {

     

    public void Choose_Color()

         {

                ***************** Your Logic******

         }

     

    }

     

     

    Monday, January 5, 2009 11:59 AM
  • agreed.

    another way to think of it is:

    class - '...is a...'
    interface - '...can do...'

    interfaces are much better over the long term of product development as it establishes a contract.  you should never change a interface after publishing it.  if you need to add a new method then you should add it to a new interface that derives from the other.  

    e.g.

    I3DEngine
    I3DEngine2


    Friday, January 9, 2009 7:02 AM
  • We use interfaces for a number of reasons.  Two of the most common reasons are;

    1. to decouple code
    2. to implement polymorphic behaviour (different objects can be used by the same code because they share an interface)

    There are a number of threads in these forums on this subject. two are below;

    http://social.msdn.microsoft.com/Forums/en-US/architecturegeneral/thread/b5c00452-3447-4545-b616-643ade1a0d6e
    http://social.msdn.microsoft.com/Forums/en-US/architecturegeneral/thread/0719c9ad-f903-4f30-b2ec-24f2dab2685a

    Pl mark as answer or helpful if you found this useful
    Wednesday, March 25, 2009 9:19 AM
  • in this paint example the concept of interface is not cleared this can be done in inheritance also why we use interface here if the car class inherit the paint property and bus class inherit the car property then this is muliti iheritance plzzzzzzzzz give me the answer i  waiting
    Sunday, July 18, 2010 2:42 PM
  • interface is a pure abstract class.in c# multi inheritance is done by with the help of interface.in interface we donot use virtual and override keywords and for better understanding of interface we will learn first what is abstract class.in abstract class we donot create objects.
    Saturday, July 31, 2010 1:23 PM
  • But in we cannot inherit multiple classess in single class but in case of interface we can, for ex-

    what if you want to impletement some feature of audi car and some of honda into another car class .

    Friday, March 14, 2014 2:27 PM
  • when you use interface , the concept of re use-ability  will come into the picture. 
    Thursday, March 27, 2014 5:06 AM
  • wow simple and nice, i could understand the benefit of interfaces finally after all these years of struggle.

    Thank you very much and appreciate your post.

    Friday, October 30, 2015 2:02 PM
  • The same  can be achieved through abstract classes, why use interface over abstract class? Multiple inheritance can be achieved through interfaces in C#, but considering other OOPS langauges  in general, why innterface when absract class can serve the purpose?
    Friday, October 30, 2015 2:06 PM
  • In theory you'd never need interfaces if your language supported multiple object inheritance. As you say pure abstract classes would be the way to go, such as in smalltalk (not that I've used it so I'm thin ice there). However, implementing language support for multiple object inheritance is tricky. As already mentioned interfaces offer a partial solution by guaranteeing the contract of the operations/properties but without any implementation or override mechanisms. However you could also do this via 'duck typing' where you forego the stronger contract for a mechanism that is arguably more cross platform, but I digress

    http://pauliom.wordpress.com

    Friday, October 30, 2015 11:05 PM