Hello All,
This is one of the most frequently common questions asked in almost every C# .NET interviews.
Sometime it sounds very silly question but hard to convivence interviewer, absolutely oops are like that.
First lets see, What is Abstract and Interface? then we will look into the real world usage.
Abstract class and interface both are used to achieve abstraction in C# Programming language.
ABSTRACT CLASS:
- Abstract class can have both abstract and non-abstract methods.
- An Abstract class can have public, private, or protected class members.
- Abstract class have constructor.
- Multiple inheritance can’t be achieved using abstract class.
- “abstract” keyword is used to declare an abstract class.
INTERFACE:
- Interface can have only abstract methods.
- Every method, properties or event inside an Interface must be public only by default.
- Interface don’t have constructor.
- Multiple inheritance can be achieved using interface.
- “interface” keyword is used to declare an abstract class.
When to go for abstract class?
Its is best practice to use abstract class when our application is having more dynamic functionality and always features are getting extended and also there is a strong relationship between base and derived class.
Ok Now let’s say we have an application that logs information different kind of formats. So we could have an Abstract class called LoggingData which has an abstract method LogInformation defined.
Now we can extend this class for different kinds of Logging- LogToTextFile, LogToSQLServer, LogToSharePoint which implement the logging method in their own way.
The advantage here is that tomorrow if this application has to Logging to Oracle database Server, we could simply extend LoggingData for a new class LogToOracleServer. In this case your abstract class is provide a base implementation for Logging Information.
If we want to upgrade the application to View the Logged Information, we can add another method GetAllLogInfo, which then can be defined in the derived class.
One more good thing is in Interface all methods must be public, but in Abstract class, we can add private, protected methods as well.
And other good thing in abstract class is no need to define any new method in base class when we define in abstract class. but in interface we need to do that.
Fun Part : I will give example for Bottle:
An interface is like the mold, it has the shape of the final product, but you can fill it with all kinds of different colors and materials. They will all have the same shape in the end, even if they are completely different in color and texture.
An abstract class would be more like something that needs an extra step, like painting. You create the basic plastic bottle, then sent it to painting department to get some painting or fur glued on or something.
When To Go for Interface?
When we do not have any change in the application or less change then its better to go for interface. Also when we have functionality every implementor should implement it interface is good practice.
For example Consider our application here again, say we want a series of methods that would save the data to a database or update the existing data or remove them. So in this case we could create an interface SaveData with save, update, delete methods that would be implemented in all the classes.
That's is for this article!!! I hope you enjoyed it. In case any concern please respond through comments.
See you in next article, Have great day....