Interfaces vs Abstract Classes

Which one should I use? I have a simple answer!

Photo by Charles Deluvio on Unsplash

Each has their strength and weaknesses. I could go over a list of things they each do, but I found an answer that helps me understand them better.

Abstract Classes

- Use Abstract Classes for when you have multiple objects that relate to each other.


- Use Interfaces for when several objects, that aren’t related, have common functionality.

For instance, an Enemy class is a great example of when to use an abstract class. Especially when you have different kinds of enemies that share the same code. Instead of programming the same code over and over again across 50 different enemies, you could have an abstract Enemy base class to share all related functionalities.

A good use case example for interfaces is when you have multiple objects that can be damaged. An Enemy can be damaged. A vehicle can be damaged. Furniture can be damaged. I hope you get the picture! They are not related by any means, but they all share common functionality. That is where an interface comes in.

So to answer the question, use both if needed!

Edit: In the comments section, Maximiliano says:

“Using inheritance for code sharing is a code smell

You should use interfaces as much as possible unless the inheritance exists in real world (enemies are not)”

Something to think about! Thank you, Maximiliano.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store