Differentiate between an Interface and an Abstract class


Interface:

  • Interfaces are closely related to abstract classes that have all members abstract.
  • All the methods of an interface must be virtual.
  • A Class that implements an interface must provide concrete implementation of all the methods definition in an interface or else must be declared an abstract class.
  • In C#, multiple inheritance is possible only through implementation of multiple interfaces.
  •  An interface defines a contract and can only contains four entities viz methods, properties, events and indexes. An interface thus cannot contain constants, fields, operators, constructors, destructors, static constructors, or types.
  • Also an interface cannot contain static members of any kind. The modifiers abstract, public, protected, internal, private, virtual, override is disallowed, as they make no sense in this context.
  • Class members that implement the interface members must be publicly accessible.
  • Interface increase security by hiding the implementation.

Abstract Class:

  • At least one method of an abstract class must be an abstract method that means it may have concrete methods.
  • Abstract class’s methods can’t have implementation only when declared abstract, otherwise they can have implementations and they have to be extended.
  • Abstract class can implement more than one interfaces, but can inherit only one class.
  • Abstract classes can only be derived once.
  • Abstract class must override all abstract method and may override virtual methods.
  • Abstract class can be used when implementing framework
  • Abstract classes are an excellent way to create planned inheritance hierarchies and also to use as non-leaf classes in class hierarchies.

No comments:

Post a Comment