C# Interfaces

What are interfaces?

An interface is functionally equivalent to having an abstract class with only public abstract members being events, getters, setters, and methods.

So, why not abstract classes then?

Interfaces can do one thing that abstract classes cannot. Interfaces can inherit from multiple interfaces whereas abstract classes can only inherit from one class.

So, why ever use abstract classes?

Well, abstract classes are still useful. You can place code needed by all subclasses in the abstract class. Sometimes, I will create my inheritance model using interfaces, and then use an abstract class to set the table for the classes that will inherit from it. This helps to adhere to the DRY principle. Don’t repeat yourself!

Well, okay… but which is better

What’s a better hand tool? A hammer or screwdriver? The answer is neither. They both have their uses, and both can be horribly misused. The same thing goes for interfaces, and abstract classes.

Author: Peter

Thank you for visiting my profile. I encourage you to get in contact with me, and follow me on GitHub : https://github.com/emancipatedMind. I look forward to collaborating with you.

Leave a Reply

Your email address will not be published. Required fields are marked *