On Java language realm, a question that shows up a lot on interviews, what’s the difference between interfaces and abstract classes? why both are needed?
Plain and simple would be:
Every class can extend only one super class (abstract class), but implement multiple interfaces, hence multiple inheritances.
Abstract classes can have constructors while on interface you can’t.
On abstract classes methods can have different access modifiers (public, private and protected) while interface only public ones.
Abstract classes can extend other abstract classes and implement multiple interfaces, while interfaces can only extends more interfaces.
Before you would say abstract classes can have concrete methods and interfaces can’t, but that changed after Java 8, now interfaces can have it as well using default keyword (keeping in mind that they will still be public, static and final).
On interfaces all methods are with public access modifier, static methods and final ones (can’t be overridden), while on abstract classes you can have all the access modifiers, can be static and non-static and final and non-final.
In conclusion I would say they are similar but different, used on different aspects.
Interfaces usually are made for firming a contract, used on factory creation classes that allows you to have a more declarative way of instances, also when you want to mock tests, or unify, generify a process, want to use something but don’t necessarily needs to know who is behind, which makes more loosely coupled, indeed needed on let’s say for example interface segregation.
Abstract classes idea is as a base class for concrete sub classes, something as default that can be overridden and at same time abstract methods that can be used on itself or on other classes (that’s why you have flexibility on the access modifiers). Again as a note, on Java 8 default keyword was introduced on interface, so you can also have default methods on interface that allows you to implementations up to an extent.
To finalize I will add a quote from Daniel Bell :)
Technology like art is a soaring exercise of the human imagination.
If I’m missing something or there is anything thing else to discuss feel free to drop a comment.