• Register
2 votes

Problem :

I am new to C# and I am currently preparing for my interview and I have below question.

When to use abstract class and interface in c# with real time example

Can somebody help me in finding correct answer for above question?

3.2k points

Please log in or register to answer this question.

1 Answer

2 votes

Solution :

This is the most frequently asked question in any C# interview. I am trying to answer it for you.

If you want to create the multiple versions of your component then you should create an abstract class. The abstract classes will provide the simple and very easy way to create different versions of your components. If you update your base class then all the inheriting classes will be  automatically updated with the change which you have made in base class. Interfaces are the blue prints and they cannot be changed once created. If for any reason a new version of the interface is required then in that case you must create the completely new interface.

If your functionality which you are creating is for a wide range of different objects then you should use the interface. The abstract classes can be used for the objects those are very closely related to each other and the interfaces should be used forgiving the common functionality for not related classes.

If you want to design the small functionality then you should use the interfaces. And if you want to  design the large functionality then you should use the abstract class.


5.7k points