Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.
Interface:
An interface can contain methods, properties, events and indexers.
Interface members must be implemented in your class.
In C#, Multiple inheritance support (we can inherit multiple interface in a class)
Abstract Class:
1. An abstract class is a class that cannot be instantiated, but must be inherited from.
2. An abstract class may be fully implemented, but is more usually partially implemented or not implemented at all,thereby encapsulating common functionality for inherited classes.
3. An Abstract Class can have abstract method and non abstract method. the abstract method must be override in derived class.
4. An Abstract class always act as base class.
5. An Abstract Class can inherit Interface. but an Interface Cannot.
Common Points:
1. Both interfaces and abstract classes are useful for component interaction.
2. Classes may inherit from only one base class, so if you want to use abstract classes to provide polymorphism to a group of classes,
they must all inherit from that class.
3. Abstract classes may also provide members that have already been implemented.
Therefore, you can ensure a certain amount of identical functionality with an abstract class, but cannot with an interface.
Here are some Recommendations to help you to decide whether to use an interface or an abstract
class to provide polymorphism for your components :-
Interface is binding of contract and a way to force to implement all the methods if you want to use that interface type. Or if you want to inherit a class from more than one types.
Abstract class is used when a class wants to provide some of the functionality and let the others to use that functionality while extending some of it or providing new implementation of it (through overriding old and abstract methods). Java and C# don't support multiple inheritance of classes to avoid Diamond problem.
Abstract Class should be used when you want to force the derived class to override only abstract methods.
Interface should be used when you want to force the class implementing interface to override all methods declared in interface.
use Abstract class only if its the only class to be inherited as C# doesn't supports multiple inheritance.
but you can implement multiple interface in a single class.