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.
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.
No comments:
Post a Comment