While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Difference between Abstract Class and Interface is one of the popular interview questions. Abstract Class and Interface are a core part of the Java programming language. Whether to choose an interface or abstract class is a design decision that every architect faces. In my last articles, I have provided as much as possible details about java interface and abstract class. In this post, we will learn about the difference between abstract class and Interface and when should we use interface over the abstract class and vice versa.
abstractkeyword is used to create an abstract class and it can be used with methods also whereas
interfacekeyword is used to create interface and it can’t be used with methods.
extendskeyword to extend an abstract class and they need to provide implementation of all the declared methods in the abstract class unless the subclass is also an abstract class whereas subclasses use
implementskeyword to implement interfaces and should provide implementation for all the methods declared in the interface.
abstractkeyword to make a class abstract but interfaces are a completely different type and can have only public static final constants and method declarations.
main()method but we can’t run an interface because they can’t have main method implementation.
That’s all for the difference between an interface and abstract classes, now we can move on to know when should we use Interface over Abstract class and vice versa.
Whether to choose between Interface or abstract class for providing a contract for subclasses is a design decision and depends on many factors. Let’s see when Interfaces are the best choice and when can we use abstract classes.
Using interfaces and abstract classes together is the best approach to design a system. For example, in JDK
java.util.List is an interface that contains a lot of methods, so there is an abstract class
java.util.AbstractList that provides a skeletal implementation for all the methods of List interface so that any subclass can extend this class and implement only required methods. We should always start with an interface as the base and define methods that every subclass should implement and then if there are some methods that only certain subclass should implement, we can extend the base interface and create a new interface with those methods. The subclasses will have the option to chose between the base interface or the child interface to implement according to its requirements. If the number of methods grows a lot, it’s not a bad idea to provide a skeletal abstract class implementing the child interface and providing flexibility to the subclasses to chose between interface and an abstract class.
From Java 8 onwards, we can have method implementations in the interfaces. We can create default as well as static methods in the interfaces and provide an implementation for them. This has bridged the gap between abstract classes and interfaces and now interfaces are the way to go because we can extend it further by providing default implementations for new methods. For more details, check out Java 8 interface default static methods.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.