Was this helpful?
At a recent interview I was asked about the difference between an interface vs and an abstract class and when to use which one.
I answered that that interfaces do not have any implementation whereas an abstract class does. So, an abstract class can have one or more be abstract members (i.e. without implementation) with other members implemented. What I did not answer really answer is when which one is preferred.
From the body language of the interviewer I felt he was not very satisfied, but I could not think of an example of when to use an interface over an abstract class or the other way around. Can someone tell me how I did and/or what I was missing?
Was this helpful?
These are some important differences.
Abstract classes are templates to other classes with basic functionality implemented. Derived classes only need implement the abstract members. Only single inheritance is supported, in other words, a derived class can only inherit from one abstract class.
Interfaces are 'skeletons' with only members defined without implementation. In other words a class that inherits from an interface must implement all members (methods and properties). Furthermore, a derived class can implement multiple interfaces. Finally interfaces are typically smaller than abstract classes. Take
|asked||May 09, 2016|
|active||May 10, 2016|
|•||Reading appsettings.json in a .NET Core console application|
|•||How do I escape a double quote in a verbatim string literal?|
|•||How to pass a parameter to a SQL IN clause with C#, SQL Server|
|•||A gentle introduction to C# Attributes|
|•||Going forward: Should I rewrite my HtmlHelpers into TagHelpers?|
|•||Creating nested TagHelpers in ASP.NET Core|
|•||C#: best practice of when to use a var vs the actual type?|
|•||Is there a difference between String and string in C#?|
|•||How to Encrypt and Decrypt a string in C#|
|•||Dynamically combining multiple OrderBy clauses in LINQ|