1

We all know that interfaces and abstract classes are needed for many design principles, and were told to be "the best practice" of programming, both for maintenance and expansion purposes.

But I also heard people saying, "Do NOT overuse interfaces or abstract classes"!

So then, how do I "draw the line"?

I know the difference between interface and abstract class (kind of).

So I'm not interested in "when to use which?", I'm interested in "when is too much"?

For example, the example given by r/PiggyChu620 from this post is clearly "overengineering" the interfaces, as r/AlarmedSlide1 down below puts it.

So to avoid the same mistake r/PiggyChu620 did, is there any "borderline guideline" as to "when should I use interface/abstract class and when should I not"?

Thank you very much for your help!

Noob002
  • 38
  • 5
  • 2
    If you ask four people about this you'll get five different answers, each considered to be the best practice. Have you read [this question](https://stackoverflow.com/questions/747517/interfaces-vs-abstract-classes)? – devsmn Nov 24 '21 at 08:50

2 Answers2

0

It's actually pretty easy.

If you can delete an interface from your code (maybe replace it with a specific class when it's used as a parameter) and your code still works, it wasn't needed.

As with every tool, the answer to the question "when should I use it" is "when you need too". If you use it "just because", then you probably make your program simpler (and therefor more maintainable) when you don't use it at all.

Interfaces, Base classes and OOP are important because you are solving problems with it. If you find yourself doing it without solving an actual problem with it, I would consider it "overused".

nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

Interfaces are contracts that are signed between classes, and all classes that use it are bound by the rules of the interface.

This type of design makes it easier to reuse and increases maintenance. Suppose you create a vehicle interface. All vehicles must implement this interface, including bicycles, motorcycles, cars, etc.

This style of design creates blood and creates a beautiful and principled architecture for your program

So if you have classes that behave similarly, you can use interfaces or abstract classes.

You can refer to this link to compare abstract classes and interfaces

Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17