• 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?

8 5 2
3,230 points

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.


8 4
5,680 points

Related questions

0 votes
1 answer 15 views
0 votes
1 answer 7 views
I am newbie to C# I am confused by .Net Garbage Collection logic. When does GC free memory?
asked Oct 8 alexh 280 points
1 vote
1 answer 8 views
What are polymorphism and inheritance? Why And When To Use "Inheritance" and "Polymorphism"?
asked Oct 4 Sofi55 1.1k points
0 votes
1 answer 26 views
Problem: I am beginner to C#. Please find below my code for your help: public class MySoundManager : MonoBehaviour { public List<AudioSource> audioSounds = new List<AudioSource>(); public double minTime = 0.5; public static void playSound(AudioClip sourceSound, Vector3 objectPosition, int ... to create the object of a class or create a methods as the static one but I don&rsquo;t know how to do it.
asked Jul 22 Raphael Pacheco 4.9k points
0 votes
1 answer 7 views
How do you convert a numerical number to an Excel column name in C# without using automation getting the value directly from Excel.?
asked Aug 28 Aliza313 720 points