This is a really busy question. They both have their application in different situations.
Abstract classes
The Abstract class is used when there are common functionalities between subclasses, but the superclass itself will never exist. For example, the Person class will never exist. However, the Woman class will be. Instead of repeating all the methods that are in Woman , in Man , Boy and Girl , we simply cast it into a superclass so that they all have access to this functionality and can override the methods that they want to perform differently.
Interfaces
Interfaces are used when there is a requirement. The way I look at them is like a contract. The convention is that any class that implements the Interface will need to provide code for a certain number of methods.
For example, class A Rock and class Ball can be thrown away. Instead of a ball-throwing method that every object that can be thrown should take into account if each object implements the ThrowingItem interface (I did not want to use the word Throwable for obvious reasons), then the method can simply accept an object of the ThrowingItem type and knows for sure that the agreed methods will be there.
This provides a weak connection between the throw method and the classes that use it, since all interaction is done through the interface.
As you can see, two different types are used in completely different situations, and comparing them is like comparing apples and oranges.
Change: I have had some time to think about the ABCs in the last few years.
Over the past few years, there has not been a single case where I needed to use inheritance in my production code. Inheritance becomes difficult to test with abstract classes. How do you test an abstract class? You cannot create an instance of it and create an instance of a subclass if these methods were overridden in a polymorphic way.
Instead, I spent all my time writing. This has led to the emergence of small sole responsibility services that are very easy to verify. So, as a rule, composition over inheritance.
christopher
source share