Wednesday, August 20, 2014

Difference between Abstract Class and Interface

1. An abstract class can have abstract members as well non abstract members. Whereas in an interface all the members are implicitly abstract.

2. A class can inherit one or more interfaces, but only one abstract class. So, with the help of Interface C# support multiple inheritance.

3. Abstract class is refererence type because it's a class. In an Interface we can't say anything until then, who is going to be implement. If a class is implement it behaves like a reference type and if a structure implement, then it behaves like value type.
e.g.  

    interface MyName                                                     
     {
         void Name();
     }

    struct StructInterface:MyName                                       public class clsInterface : MyName
    {                                                                                      {
         public void Name()                                                          public void Name()
         {                                                                                         {
             Console.Write("My Name");                                               Console.WriteLine("My Name");
         }                                                                                         }
    }                                                                                       }

4. Abstract class provide partial abstraction while Interface provide full abstraction.

5. Generally, abstract class used where, we have partially idea about the thing, that somewhere we confirm over the logic and somewhere not. But in the case of Interface we don't have any idea related the thing.





No comments:

Post a Comment