ALL >> Education >> View Article
Difference Between Interface And Abstract Class In Java
Total Articles: 250
What is abstract class in Java
An abstract class is something which is incomplete and you can not create an instance of the abstract class. If you want to use it you need to make it complete or concrete by extending it. A class is called concrete if it does not contain any abstract method and implements all abstract method inherited from abstract class or interface it has implemented or extended. By the way Java has a concept of abstract classes, abstract method but a variable can not be abstract in Java.
Even though both interface and abstract class is 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 focus 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 abstract class is 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 difference between interface and abstract class in Java programming language and later we will see where to use abstract class and interface.
Though, if you are preparing for Java programming interview, you should also check out the Java Programming Interview Exposed, a great book specially designed to prepare for Java concepts based interview questions. It has many such questions from all important topics e.g. oop concepts, multithreading, collections, frameworks like Spring and Hibernate, unit testing, data structure and algorithm, coding, design pattern and modern technology questions like Android, Scala, and other JVM languages.
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.
1) First and the major difference between abstract class and an interface is that an abstract class is a class while the interface is an interface, means by extending the abstract class you can not extend another class because Java does not support multiple inheritances but you can implement multiple inheritance in Java.
2) The second difference between interface and 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 abstract class. Even a class which doesn't contain any abstract method can be made abstract by using the abstract keyword.
3) 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. The Effective Java has one item dedicated to explaining about why you should be using interface for type declaration. You should check that out as well.
4) The fourth difference between abstract class and interface in Java is that abstract class are slightly faster than interface because 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) Another notable difference between interface and abstract class is that when you add a new method in 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.
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 default method in Java 8 (See Java 8 in Action) and the provision that you can have both static and default method inside an interface, the difference between abstract class and interface has become blur. Earlier, the interface only contains contract no implementation but now they can.
Education Articles1. Interior Design Course In Chennai
2. Software Testing Institute In Chennai
Author: Ganesh Kumar
3. I Owe My Success To The Top Engineering College; Jmit
Author: ponting brown
4. Pass Microsoft Dynamics 365 Mb6-895 Exam In First Attempt With Valid Mb6-895 Exam Questions
Author: Bella Henry
5. Study Mbbs In Ukraine- One-stop-place To Study Mbbs
Author: kamal sharma
6. Why 2013 Is The Year Of Php
7. Best Web Designing Tools (2018)
8. Take The Help Of College Counseling For Getting Admission Abroad
Author: Michelle andre
9. Aspiring To Study Abroad? Take Up The Pte First Before Applying For Student Visa
Author: Mia Seal
10. Engineering In Artificial Intelligence
Author: Gaurav Peshkar
11. How Pmp Training Program Is Beneficial For Your Career?
12. Best Digital Marketing Course In Hyderabad
Author: sai bhargav
13. How Nbfc Companies In Mumbai Is Changing Things For Student Loan:
Author: Eduvanz provides low cost, smart loans to finance
14. Common Mistakes A Graphic Designer Must Always Avoid...
Author: pankaj singh
15. Career Scope In Animation
Author: Visual Media