When designing my code, I'm trying to understand whether it's better to define common functionality in an abstract class or use a default method in an interface. What are the pros and cons of each approach?
3 Answers
I’d suggest steering clear of default methods in interfaces when you need common functionality. Abstract classes are more suited for that purpose.
There’s really no one-size-fits-all answer, but using interfaces might be the way to go. Since a class can only extend one parent class, you might want to reserve parent classes for specific needs. If you do end up using one, it makes sense to incorporate shared functionality there.
It really depends on your situation. If the classes share common functionality, go for an abstract class. But if they just share a behavior without any common functionality, like all organisms move differently, an interface would be better.
Why is it better to avoid default methods?