Interface or abstract class | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Interface or abstract class

When you are working on a project and you are confused between implementing interface or working with abstract classes, what is preferably used and why?

5th Apr 2020, 3:47 PM
Dhafer ABDELKADER
Dhafer ABDELKADER - avatar
1 Answer
+ 4
Depends on what level of abstraction you want to achieve. Abstract classes can have both abstract and non-abstract methods which can contain common implementation for all your subclasses. Interface on the other hand is useful when you want all of your implementing classes to have their own implementation of the abstract method declared in the interface. Interface also have default and static methods which can have the method implementation within the interface. So there isn't much of a difference but it is advised to use abstract classes in most cases.
5th Apr 2020, 5:55 PM
Avinesh
Avinesh - avatar