Posted by jineesh uvantavida on Monday, June 7, 2021
Abstract Class
- Its a way to achieve abstraction in c#.
- Not instantiated directly
- must contain at least one abstract method.
- used to define base class.
- contains both declaration and definition.
- contains constructor.
- it can contain static members
- contains different types of access modifiers like public, private, protected.
- performance of abstract class is fast.
- used to implement core identity of class
- a class can only use one abstract class.
- if many implementation are same kind and common behavior, then it is superior to use abstract class.
- it can be fully, partially or not implemented.
Interface
- can have methods, properties, events and indexers as its members
- only declaration of the members.
- implementation of interface member is done by the class who implements the interface.
- multiple inheritance is achieved by interface.
- it does not contain constructor
- it does not contain static members
- contains only public access modifier because everything in the interface is public
- performance is slow because it requires time to search actual method in the corresponding class
- used to implement peripheral abilities of class
- a class can have multiple interface.
- if many implementation share only methods, then it is superior to use interface
- it should be fully implemented
Tags:
difference between abstract class and interface in c#