In What Scenarios Will You Use Abstract Class vs Interface

In what scenarios will you use an abstract class and in what scenarios will you use an interface?

  • If you want to increase reusability in inheritance then abstract classes are good.
  • If you want to implement or force some methods across classes must be for uniformity you can use an interface.
  • So to increase reusability via inheritance use the abstract class as it is nothing but a base class and to force methods to use interfaces.
Some points about Abstract Class:
  1. We cannot create an object of an Abstract Class.
  2. An Abstract Class can be inherited with its derived class.
  3. We can have both concrete and abstract methods but at least one abstract method is compulsory in an Abstract Class.
  4. An Abstract Class is used as a base class for projects.
  5. An Abstract Class can inherit another base class and base interfaces.
  6. The constructor of an Abstract Class can be called through the derived class constructor.
  7. An Abstract Class should be used for a large project to share the common functionality with its derived class.
  8. All abstract methods must be implemented in its derived class if inherited.

Some important things about interfaces

  1. We cannot create an object of an interface; we can only create a reference or Interface Variable.
  2. An interface will have only abstract methods (method declaration).
  3. Interfaces support Inheritance, Polymorphism (Overloading, Overriding (using the "new" keyword to hide the interface methods above)).
  4. We cannot create variables in an interface.
  5. Only Properties, Indexers, Methods and Events are allowed in an interface.
  6. A class can inherit multiple interfaces, which is also shown in the snapshot above.
  7. We cannot create any Access modifiers with Interface Members (like private, public, protected, internal, protected internal, virtual, override, static, abstract etc.)
  8. The new keyword is allowed in an interface.
  9. All methods of an interface must be defined in every derived class.
  10. An interface is good for small or medium level projects.
Abstract ClassInterface
  1. An abstract class can have a constructor declaration
  1. While an interface cannot do so.
  1. An abstract class is allowed to have all access modifiers for all of its member declarations
  1. While in the interface we cannot declare any access modifier (including public) as all the members of an interface are implicitly public.
  1. An abstract class can declare or use any variables
  1. While an interface is not allowed to do so.
  1. An abstract class can have non-abstract Methods(concrete methods)
  1. While in the case of Interface all the methods have to be abstract.
  1. In an abstract class, all data member or functions are private by default
  1. While in interface all are public, we can’t change them manually.
 
Main Difference

An Abstract Class should be used for a large project to share the common functionality with its derived class.

An interface is good for small or medium level projects.