What are the difference between interfaces and abstract classes

There are several differences between an abstract class and an interface as listed below.

Here is a youtube video on difference between abstract classes and interfaces


1. Abstract classes can have implementations for some of its members, but the interface can't have implementation for any of its members.

2. Interfaces cannot have fields where as an abstract class can have fields.

3. An interface can inherit from another interface only and cannot inherit from an abstract class, where as an abstract class can inherit from another abstract class or another interface.

4. A class can inherit from multiple interfaces at the same time, where as a class cannot inherit from multiple classes at the same time.

5. Abstract class members can have access modifiers where as interface members cannot have access modifiers.

Another common C# Interview Question, that is commonly asked is, When do you choose interface over an abstract class or vice versa?
A general rule of thumb is, If you have an implementation that will be the same for all the derived classes, then it is better to go for an abstract class instead of an interface. So, when you have an interface, you can move your implementation to any class that implements the interface. Where as, when you have an abstract class, you can share implementation for all derived classes in one central place, and avoid code duplication in derived classes.

15 comments:

  1. what are the methods used in functions in c#?

    ReplyDelete
  2. virtual keyword in c#?
    private constructor in c#?

    ReplyDelete
    Replies
    1. Virtual Keyword in C#: If you mark a method as virtual, it can be overriden in the derived class. A practical example for this would be, to have a default implementation in the base class and any derived class can override with its very own version of implementation. If you want a sample C# Program please read this article. The Draw() method is a virtual method, in the base DrawingObject class. But the same method is overriden in the derived classes (Traingle, Circle, Square) as per their respective requirements. Properties, events, and indexers can also be marked as virtual.

      Private Constructors in C#: There are several reasons for using private constructors
      1. If you want the caller of the class only to use the class but not instantiate.
      2. If you want to ensure a class can have only one instance at given time, i.e private constructors are used in implementing Singleton() design pattern.
      3. If a class has several overloads of the constructor, and some of them should only be used by the other constructors and not external code.

      Delete
  3. Plz give the practical explanation of above mentioned abstract class and interface?

    ReplyDelete
  4. When do you choose interface over an abstract class or vice versa?
    Hi
    venkat
    can u explain it clearly by using some programing code it better to understand

    ReplyDelete
  5. "If you have an implementation that will be the same for all the derived classes, then it is better to go for an abstract class instead of an interface."
    Even if I don't have the same implementation, I can still go for abstract class by declaring the methods as abstract. Can you please emphasize more or explain a scenario where I would really prefer an interface over an abstract class.. I have been trying to find a concrete answer for this but in vain.. one of the advantages of using Interface is obviously Multiple inheritance as you have already explained..but am sure there has to be better answer to this...

    ReplyDelete
    Replies
    1. "If you have an implementation that will be the same for all the derived classes, then it is better to go for an abstract class instead of an interface." but we cannot implement in abstract class.

      Delete
    2. "If you have an implementation that will be the same for all the derived classes, then it is better to go for an abstract class instead of an interface." but we cannot implement in abstract class.

      Delete
    3. lets consider a mobile as an example.

      common features- switching on,switching off,standby,ring.---->can be done using abstract class.


      number of screens supported,audio_output,contacts storing(format),etc----> these things can be done using interface.
      i.e. these functionalities are dependent on the hardware used for the mobile, and its a vendor(client) implementation i.e.interface tells the functionalities to be implemented.

      Delete
    4. Even if I don't have the same implementation, I can still go for abstract class by declaring the methods as abstract - If you have only Abstract method inside your abstract method then it is better to go for Interfaces, because a class can implement multiple interfaces.

      Delete
  6. Can you please give a detailed explanation with some code on the question--- When do you choose interface over an abstract class or vice versa?

    ReplyDelete
  7. wht is static class in C#

    ReplyDelete
  8. Static Class- Its a class which can contain only static member in it and we can't create the object of static class, bcoz it doesn't have any instance member in it.

    ReplyDelete
  9. Hi,
    if we have situation like, Interface and Abstract class contain exactly same abstract method declaration and no other concrete methods or anything( both are exactly same ) then which one have to choose and why ?

    ReplyDelete
  10. Hi All,
    Hope this clears to choose one over another:
    1. Use Abstract classes when you have "is a" relationship and use interfaces when you don't have "is a" relationship but each type "is able" to do something.
    for example: A human is not a duck and a duck is not a human. However, both a duck and a human has 'the ability' to swim.

    2. Use Abstract classes when want to implement some of the 'default' behavior, that can be overrided in child class and use interfaces when you don't have or want to prevent a 'default' implementation.

    ReplyDelete

If you are aware of any other C# questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us