By Pankaj Kumar
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.
abstract
keyword is used to create an abstract class and it can be used with methods also whereas interface
keyword is used to create interface and it can’t be used with methods.extends
keyword 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 implements
keyword to implement interfaces and should provide implementation for all the methods declared in the interface.abstract
keyword 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.
Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev
Hello Pankaj, I like this post much. I got clear idea about when to use interfaces and when to use abstract class. But i didnt understand last paragraph i.e extending interface. could u elobarate more. So that it will understandable. I will be clear if u provide some bit of code. Please Thanks.
- sunil patil
Hi Pankaj Sir, Thank You for sharing this wonderful site with us. Would be Thankful to you,if you can shed light on ANT,JUnit,TestNG topics too. I am an Automation tester in Selenium and am looking forward for the topics mentioned above,since,your experience and understanding helps us understand the topics better. Thanks Again. ~Chaitanya
- Madiraju Krishna Chaitanya
you mentioned in last point"Interfaces are used to define contract for the subclasses" like what is ’ contract here’ klindly explain that point
- lakshmi
sir can u give me reply for the question i have asked on last point
- lakshmi
sir 8. interface can only extend other interfaces. but interface only implements other interface correct
- sha
the best explanation i came across. going to copy this page forever.
- rishi
"… Also if subclasses don’t need to implement particular method, they can avoid providing the implementation… " - does that mean we can choose NOT to implement any method of the abstract class?
- abhishek chanda
What is the simple definition of abstract?? Give me an example…
- Waseem Ahmed Kalhoro
Hi. I have a question. Assume that there is an abstract class with method String Name() and an Interface with the same method String Name(). The signatures of both methods in abstract class and interface is same. Now Class A extends the abstract class and implements the interface. Which String Name() method is implemented in Class A??? Example : //AbstractClass public abstract AbstractClass { public abstract String Name(); } //Interface Public interface SampleInterface{ public String Name(); } // which method is implemented here??? public class A extends AbstractClass implements SampleInterface{ public String Name(){ //Implementation // Which method is implemented here. The abstract class method or interface method??? } I tried executing a small sample program. The program runs fine but i am not sure as to which method is implemented here.
- Shwetha
You have written “The subclasses will have option to chose between the base interface or the child interface to implement according to its requirements” Hows this possible ?
- Aliasger Motiwala
Hi Pankaj, Good Explanation on Abstract and Interfaces. Can you Post Tutorials on MAVEN, LOG4J and JUnit. Thanks
- KALADHAR KHAMITHKAR
You mention that abstract class can be run throw main method while interface can’t However abstract class cant be instantiated so how JVM is going to run the abstract class. You can call any static method with Abstract class implementation, No matter it is main method or not.
- Oshin Talreja
Can abstract methods in an abstract class be private? No right?
- Vikas Palakurthi
Hi Pankaj, I think “Subclasses use extends keyword to extend an abstract class and they need to provide implementation of all the declared methods in the abstract class” is not correct. It should be "Subclasses use extends keyword to extend an abstract class and they need to provide implementation of all the declared abstract methods in the abstract class "
- Stan
Great Explanation, Your article provides quality content on difference between Abstract class and Interface. It is a very common question usually asked in interviews. I agree with your all points. We can achieve Abstraction 0-100% through abstract class and 100% abstraction from Interfaces because Interface can contain only abstract methods but Abstract class may have abstract method as well as methods with implementation. I am also running a blog (java2blog) which is tutorial on Java. It also provides useful interview questions and answers on Java.
- Arpit Mandliya
We can run interface from Java8 as it can have static method and we can execute so the difference of not running interface is invalid after java8, either u can update or add.
- Chetan
Nice and very straight forward post. Thanks for posting.
- GST Billing Software
Dear Pankaj, Please tell me where we use abstract class and where we use interface in java project.
- Vivek Dubey
Just a friendly reminder, I’ve noticed that you mistakenly used “chose” in many articles where “choose” should be used in fact. Nonetheless, this is a great set of tutorials on Core Java :)
- Kenneth
Hi , first of all your every post is useful . I have one request , can you change the difference between abstract class and interface according to java 8 changes
- annonymous
Hi Pankaj, Nowadays many sites for explanations but your blog quite impressive because few points added more and those are very helpful for interview point of view. Thanks a lot
- Hari
“We can run an abstract class if it has main() method but we can’t run an interface because they can’t have main method implementation.” Actually we can do it starting from Java 8 since main method is static.
- Max
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.