What is abstract class and interface in Java?
The difference between abstract class and interface in Java is one of the tricky Java interview questions and mostly appears in core Java interviews. It has become now even trickier after Java 8 introduced default methods and allowed interfaces to have both default and static methods. Even though both interface and abstract class are a way to achieve abstraction in Java, there are significant differences between them, which you will learn in this article. Some time interviewer also not just focuses on key differences between abstract class and interface in Java but he is also interested in some practical experience e.g. when to use interface in Java and when to use an abstract class in Java.
This is actually the tricky part of this interview question and you must have a good understanding of what is an interface and abstract class in Java and how to use them. Anyway in this Java article, we will first see some syntactical differences between an interface and abstract class in Java programming language and later we will see where to use abstract class and interface.
However, if you are preparing for a Java programming interview, you should also check out these Java Interview Preparation Courses, a great collection of resources specially designed to prepare for Java concepts-based interview questions. It has many such questions from all important topics like oop concepts, multithreading, collections, generics, new Java features, Functional Programming, Lambda, Stream, and other core Java topics.
The difference between abstract class and interface in Java is one of the tricky Java interview questions and mostly appears in core Java interviews. It has become now even trickier after Java 8 introduced default methods and allowed interfaces to have both default and static methods. Even though both interface and abstract class are a way to achieve abstraction in Java, there are significant differences between them, which you will learn in this article. Some time interviewer also not just focuses on key differences between abstract class and interface in Java but he is also interested in some practical experience e.g. when to use interface in Java and when to use an abstract class in Java.
This is actually the tricky part of this interview question and you must have a good understanding of what is an interface and abstract class in Java and how to use them. Anyway in this Java article, we will first see some syntactical differences between an interface and abstract class in Java programming language and later we will see where to use abstract class and interface.
However, if you are preparing for a Java programming interview, you should also check out these Java Interview Preparation Courses, a great collection of resources specially designed to prepare for Java concepts-based interview questions. It has many such questions from all important topics like oop concepts, multithreading, collections, generics, new Java features, Functional Programming, Lambda, Stream, and other core Java topics.
Abstract class vs Interface in Java
In the last section, we saw what is abstract class and interface and now let's see the difference between interface and abstract class in Java. While Java 8 enhancements which allow concrete methods like default and static method on the interface has reduced the difference between an interface and abstract class in Java, there is still some worth noting difference which every Java developer should be aware of.1. Class vs Interface
The first and the major difference between an abstract class and an interface is that an abstract class is a class while the interface is an interface, which means by extending the abstract class you can not extend another class because Java does not support multiple inheritances but you can implement multiple inheritances in Java.
2. Non-Abstract Methods
The second difference between an interface and an abstract class in Java is that you can not create a non-abstract method in an interface, every method in an interface is by default abstract, but you can create a non-abstract method in the abstract class.
Even a class that doesn't contain any abstract method can be made abstract by using the abstract keyword.
The worth noting thing is here is that after Java 8 you can create non-abstract or concrete methods on interfaces like default and static methods but you cannot create a non-default and non-static method that is not abstract. You can learn more about Java 8 and other key Java features on a comprehensive course like The Complete Java Masterclass, which I highly recommend to every Java developer.
3. Usage
The third difference between abstract class vs interface in Java is that interface is better suited for Type declaration and abstract class is more suited for code reuse and evolution perspective. Effective Java has one item dedicated to explaining why you should be using an interface for type declaration. You should check that out as well.
4. Method Resolution
The fourth difference between abstract class and interface in Java is that abstract classes are slightly faster than the interface because the interface involves a search before calling any overridden method in Java. This is not a significant difference in most of the cases but if you are writing a time-critical application then you may not want to leave any stone unturned.
5. Interface Evolution
Another notable difference between interface and abstract class is that when you add a new method in the existing interface it breaks all its implementation and you need to provide an implementation in all clients which is not good.
By using an abstract class you can provide a default implementation for a new method in the superclass without breaking existing clients.
This point is worth noting for people working on older Java versions but from Java 8 onwards, interface evolution is much easier as you can add new methods as default and static which are concrete methods. In fact, JDK has used this technique to add new methods on several key interfaces like Map or Collection in Java.
Here is a nice summary slide highlighting key differences between an abstract class and interface in Java:
That's all on the difference between abstract class and interface in Java, I will add more differences whenever I learn new things. As I said, in the first paragraph, after the introduction of the default method in Java 8 (See What's New in Java 8) and the provision that you can have both static and default methods inside an interface, the difference between abstract class and interface has become a blur. Earlier, the interface only contains contracts with no implementation but now they can.
Other Java tutorials and Resources you may like
Here is a nice summary slide highlighting key differences between an abstract class and interface in Java:
That's all on the difference between abstract class and interface in Java, I will add more differences whenever I learn new things. As I said, in the first paragraph, after the introduction of the default method in Java 8 (See What's New in Java 8) and the provision that you can have both static and default methods inside an interface, the difference between abstract class and interface has become a blur. Earlier, the interface only contains contracts with no implementation but now they can.
Other Java tutorials and Resources you may like
- 30 OOP Concepts Interview Questions with Answers
- 5 differences between Hashtable and HashMap in Java
- 6 Free Object-Oriented Programming Courses
- 7 Courses to learn OOP Design Patterns in Java
- 18 Java Design Pattern Interview Questions
- 10 tough Core Java interview questions and answers
- Top 5 thread questions and answers asked in Java Interviews
- What is the class file in Java - How to create Class in Java
- 10 Java Coding Interview Questions and Answers for Java beginners.
Thanks for reading this article so far. If you like this Abstract class vs interface question and object-oriented programming tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
P. S. - If you are serious about learning object-oriented programming and looking for a free online course to start with then you can also check this FREE Object Oriented Programming (OOPs) for the JAVA Interviews course on Udemy. It's completely free and you just need a free Udemy account to join this course.
Great post - very informative. I found this page to be very helpful as well:
ReplyDeletehttp://www.programmerinterview.com/index.php/java-questions/interface-vs-abstract-class/
Thanks dude really helpful ....
ReplyDeletethanks a lot
ReplyDeletei lerned very well by IT??????????
ReplyDeleteThank u... It s very useful for me.
ReplyDeletei don't understand where we use interface and where we use abstract class ?
ReplyDeleteUse abstract class if you want to provide a Skeleton class e.g. AbstractList class from java.util package, while use interface to define public API e.g. List is an interface and AbstractList is an abstract class. The List allows you to use List in a flexible way and AbstractList allows developer to create a new implementation of List easily.
Delete1. A class can implement more than one interface(Multiple Inheritance is possible with interface). But A class can not extend more than one abstract class(Multiple Inheritance is not possible with class).
ReplyDelete2. If You want to add a new method in interface, you need to implement that method in all the classes that implementing the interface. But if you want to add a new method in abstract class, you can add it as a non abstract method. You don't need to implement it in all the classes that extending the abstract class.
3. Interface don't have constructors. But Abstract class has constructors.
Prathap
Java training in chennai
If we don't want to allow anybody to create the object to our class,then we should go for abstract class.
ReplyDeleteEx:HttpServlet.
Indeed, but that's the half of the story, you want others to extend your class before use, that's the meaning of abstract class e.g. your servlet should extend HttpServlet and implement doGET(), doPost() methods etc.
DeleteThis is all going to change in Java 1.8. the introduction of Functional interfaces changes the differences listed here :)
ReplyDelete@Anonymous, From Java 8 Onwards interface is not completely abstract i.e. they can also have code in form of static and default methods. That actually blur the difference between abstract class and interface but still you can implement multiple interface but only extend one class.
DeleteIn an interface we cannot define static methods and can only define public static final
ReplyDeletevariables
@Anonymous, from Java 8 onward, interface can include static methods.
DeleteTypo found, there in the picture, should read: "To declare an interface, use interface keyword".
ReplyDelete@Aulo, good catch, thanks for pointing out, indeed "interface" is the keyword to declare interface in Java.
DeleteHi Javin,
ReplyDeleteCan you please explain the real time scenario when we use interface and abstract class.
I'm not clear with the answer given to Raman.
Thanks a lot
Hi Javin,Can you please update all your blog as per current java changes.
ReplyDeleteYes, its needed, given now you can add both static and default methods on inteface which is not abstract.
DeleteI have been a Java developer for 8 years now. Really well written article on abstract class and interface
ReplyDeleteThanks Ask
DeleteYour explnations is superb , but you write same sentences in first 2 paras again and again and it waste a lot of time ...
ReplyDeleteNoted, thx for feedback.
Deletei think this page must be updated. Java allows you to create non abstract method inside interface too
ReplyDeleteYes, that's correct, given now you can add both static and default methods on interface which is not abstract. I will update, its been pending from quite sometime now.
Delete